CAction.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include "stdafx.h"
  2. #include "CAction.h"
  3. namespace GameAssist
  4. {
  5. #define SETGAMEFOREGROUND AutoThreadSection ats(&m_ts);\
  6. if (!::IsWindow(m_hGameWnd)) return;\
  7. ::ShowWindow(m_hGameWnd, SW_SHOWNORMAL);\
  8. ::SetForegroundWindow(m_hGameWnd);
  9. ThreadSection CAction::m_ts;
  10. VOID DebugLog(CHAR* pszStr, ...)
  11. {
  12. char szData[MAX_PATH] = { 0 };
  13. _stprintf_s(szData, _T("%s %s "), _T("[GameAssist] "), CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S")).GetString());
  14. int len = strlen(szData);
  15. va_list args;
  16. va_start(args, pszStr);
  17. _vsnprintf_s(szData + len, MAX_PATH - len, MAX_PATH - len, pszStr, args);
  18. va_end(args);
  19. strcat_s(szData, "\n");
  20. OutputDebugStringA(szData);
  21. }
  22. CAction::CAction(HWND hWnd) :m_hGameWnd(hWnd)
  23. {
  24. }
  25. CAction::~CAction()
  26. {
  27. }
  28. void CAction::MouseMove(POINT pt, bool bSetCurPos)
  29. {
  30. SETGAMEFOREGROUND;
  31. GameAssist::GamePos2Screen(m_hGameWnd, pt);
  32. GameAssist::MouseMove(pt);
  33. }
  34. void CAction::MouseClick()
  35. {
  36. SETGAMEFOREGROUND;
  37. GameAssist::MouseClick();
  38. }
  39. void CAction::MouseClick(POINT pt)
  40. {
  41. SETGAMEFOREGROUND;
  42. GameAssist::GamePos2Screen(m_hGameWnd, pt);
  43. GameAssist::MouseClick(pt);
  44. }
  45. void CAction::MouseDbClick()
  46. {
  47. SETGAMEFOREGROUND;
  48. GameAssist::MouseDbClick();
  49. }
  50. void CAction::MouseDbClick(POINT pt)
  51. {
  52. SETGAMEFOREGROUND;
  53. GameAssist::GamePos2Screen(m_hGameWnd, pt);
  54. GameAssist::MouseDbClick(pt);
  55. }
  56. void CAction::MouseRClick()
  57. {
  58. SETGAMEFOREGROUND;
  59. GameAssist::MouseRClick();
  60. }
  61. void CAction::MouseRClick(POINT pt)
  62. {
  63. SETGAMEFOREGROUND;
  64. GameAssist::GamePos2Screen(m_hGameWnd, pt);
  65. GameAssist::MouseRClick(pt);
  66. }
  67. void CAction::MouseDrag(POINT pt)
  68. {
  69. SETGAMEFOREGROUND;
  70. GameAssist::GamePos2Screen(m_hGameWnd, pt);
  71. GameAssist::MouseDrag(pt);
  72. }
  73. void CAction::SendKey(DWORD key, BOOL bCtrl, BOOL bAtl, BOOL bShift)
  74. {
  75. SETGAMEFOREGROUND;
  76. GameAssist::SendKey(key, bCtrl, bAtl, bShift);
  77. }
  78. void CAction::SetGameForeground()
  79. {
  80. AutoThreadSection ats(&m_ts);
  81. if (!::IsWindow(m_hGameWnd)) return;
  82. ::ShowWindow(m_hGameWnd, SW_SHOWNORMAL);
  83. ::SetForegroundWindow(m_hGameWnd); // 窗口前置才能单击成功;
  84. }
  85. bool CAction::IsWalkStop()
  86. {
  87. if (!::IsWindow(m_hGameWnd))
  88. return false;
  89. // 将鼠标移走,不要在抠图区域;
  90. // 截图,抠指定区域;
  91. // 再截图,抠指定区域与上次抠图结果比较;
  92. // 比较结果相同,表示停止走路;
  93. //POINT pt1 = { 635, 20 };
  94. //POINT pt2 = { 780, 38 };
  95. CRect rc = { 630, 40, 800, 65 };
  96. CRect rc2 = { 640, 45, 770, 63 };
  97. for (int i = 0; i < 100; i++)
  98. {
  99. std::string strImg = GameGlobal::BuildImgPath(m_hGameWnd, "walk1");
  100. std::string strImg2 = GameGlobal::BuildImgPath(m_hGameWnd, "walk2");
  101. ImgAssist::CropPicture(m_hGameWnd, rc, strImg.c_str());
  102. Sleep(600);
  103. ImgAssist::CropPicture(m_hGameWnd, rc2, strImg2.c_str());
  104. // 判断2张图片是否一样;
  105. if (ImgAssist::IsSimilarPicture(strImg.c_str(), strImg2.c_str()))
  106. {
  107. DebugLog(_T("停止走路"));
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. bool CAction::IsBattle()
  114. {
  115. CRect rc;
  116. SRAND(100, 200);
  117. if (ImgAssist::IsMatchIcon(m_hGameWnd, GameGlobal::g_strAppdir + _T("img\\活动\\镜妖\\非战斗中.bmp"), { 630, 25, 805, 45 }, rc)) {
  118. DebugLog(_T("Battle:非战斗中"));
  119. return false;
  120. }
  121. DebugLog(_T("Battle:战斗中"));
  122. return true;
  123. }
  124. CRect CAction::FindMatchIcon(LPCTSTR lpszTemplateImage)
  125. {
  126. CRect rc;
  127. TCHAR szTemplateImage[MAX_PATH] = { 0 };
  128. // 先截图;
  129. DebugLog(_T("FindMatchIcon=%s"), lpszTemplateImage);
  130. ImgAssist::CaptureGameWnd(m_hGameWnd); SRAND(200, 500);
  131. _stprintf_s(szTemplateImage, _T("%s%s"), GameGlobal::g_strAppdir.c_str(), lpszTemplateImage);
  132. if (ImgAssist::GetImgMatchtemplate(GameGlobal::BuildImgPath(m_hGameWnd, _T("Game")), szTemplateImage, rc)) {
  133. // 找到匹配的模块;
  134. DebugLog(_T("====> 找到匹配目标:%s"), lpszTemplateImage);
  135. return rc;
  136. }
  137. rc.SetRectEmpty();
  138. DebugLog(_T("<==== 没找到匹配目标:%s"), lpszTemplateImage);
  139. return rc;
  140. }
  141. bool CAction::IsOnMap(LPCTSTR lpszTemplateImage)
  142. {
  143. CRect rc = FindMatchIcon(lpszTemplateImage);
  144. if (rc.IsRectEmpty() || rc.IsRectNull())
  145. return false;
  146. return true;
  147. }
  148. void CAction::WorldMapPositioning(POINT ptMap)
  149. {
  150. CRect rc;
  151. //// 打开世界地图;
  152. SendKey(VK_TAB); SRAND(300, 500);
  153. // 判断是否打开了地图;
  154. if (!ImgAssist::GetImgMatchtemplate(m_hGameWnd, GAssist::g_strAppdir + _T("img\\世界地图.bmp"), rc))
  155. return;
  156. //// 单击寻路窗口;
  157. MouseClick(ptMap); SRAND(300, 600);
  158. //// 退出世界地址;
  159. GameAssist::SendKey(VK_TAB); SRAND(200, 300);
  160. IsWalkStop();
  161. }
  162. bool CAction::IsWattingAttack(int nAttackNPCType)
  163. {
  164. CRect rc;
  165. BOOL bIsAttack = FALSE;
  166. switch (nAttackNPCType)
  167. {
  168. case 0:
  169. { // 将鼠标移动到指定位置;
  170. DebugLog(_T("IsWattingAttack:镜妖中……"));
  171. POINT ptDest = { 20, 50 }; // 左上角;
  172. MouseMove(ptDest);
  173. if (ImgAssist::IsMatchIcon(m_hGameWnd, GameGlobal::g_strAppdir + _T("img\\我方攻击.bmp"), { 0, 50, 60, 115 }, rc)) {
  174. DebugLog(_T("IsWattingAttack:我方攻击"));
  175. bIsAttack = TRUE;
  176. }
  177. break;
  178. }
  179. case 1:
  180. {
  181. // 将鼠标移动到指定位置;
  182. DebugLog(_T("IsWattingAttack:职业挑战中……"));// 要求角色:自定全黑头像;
  183. //POINT ptDest = { 625, 28 }; // 左上角;
  184. //GAssist::MouseMove(hWnd, ptDest);
  185. if (!ImgAssist::IsMatchIcon(m_hGameWnd, GameGlobal::g_strAppdir + _T("img\\战斗中.bmp"), { 600, 25, 655, 80 }, rc)) {
  186. DebugLog(_T("IsWattingAttack:我方攻击"));
  187. bIsAttack = TRUE;
  188. }
  189. break;
  190. }
  191. default:
  192. break;
  193. }
  194. return bIsAttack;
  195. }
  196. bool CAction::ClickDlgSetup(LPCSTR lpszTemplateImage)
  197. {
  198. CRect rc = FindMatchIcon(lpszTemplateImage);
  199. if (rc.IsRectEmpty() || rc.IsRectNull())
  200. return false;
  201. // 单击目标;
  202. MouseClick(CPoint((rc.right + rc.left) / 2 + rand() % 3, (rc.top + rc.bottom) / 2 + rand() % 3));
  203. // 单击后,系统切图可能要的时间会比较长;
  204. SRAND(350, 600);
  205. // 单击目标后,要往移走鼠标,防止下面匹配的时候被鼠标挡住;
  206. MouseMove(CPoint(rc.top - RAND(5, 12), rc.left - RAND(5, 10))); // 移动到左上角;
  207. SRAND(300, 500);
  208. return true;
  209. }
  210. bool CAction::FindNPCByWorldMap(LPCTSTR lpszNPCName)
  211. {
  212. CRect rc;
  213. //// 打开世界地图;
  214. SendKey(VK_TAB); SRAND(500, 600);
  215. if (!ImgAssist::GetImgMatchtemplate(m_hGameWnd, GameGlobal::g_strAppdir + _T("img\\寻路窗口.bmp"), rc))
  216. return false;
  217. //// 单击寻路窗口;
  218. MouseClick( CPoint((rc.right + rc.left) / 2 + rand() % 2, (rc.top + rc.bottom) / 2 + rand() % 2)); SRAND(500, 800);
  219. //// 设置剪切板:职业大挑战;
  220. GameAssist::SetClipboardString(lpszNPCName); SRAND(200, 500);
  221. //// Ctrl+V
  222. SendKey(0x56, TRUE); SRAND(300, 500);
  223. // 先移动到目标后,才能实现单击;
  224. MouseMove(CPoint((rc.right + rc.left) / 2 + 5 + rand() % 2, (rc.top + rc.bottom) / 2 + 60 + rand() % 2)); SRAND(200, 300);
  225. //// 寻路窗口往下50就是目标;
  226. MouseDbClick(CPoint((rc.right + rc.left) / 2 + 5 + rand() % 2, (rc.top + rc.bottom) / 2 + 60 + rand() % 2)); SRAND(200, 300);
  227. //// 退出世界地址;
  228. SendKey(VK_TAB); SRAND(200, 300);
  229. // 直到停止走动为止;
  230. IsWalkStop();
  231. // 遍历3次;
  232. bool bFind = false;
  233. for (int i = 0; i < 3; i++)
  234. {
  235. Sleep(600 - i * 150);
  236. rc = FindMatchIcon(_T("img\\对话框关闭按钮.bmp"));
  237. if (!rc.IsRectEmpty() && !rc.IsRectNull())
  238. {
  239. bFind = true;
  240. break;
  241. }
  242. }
  243. return bFind;
  244. }
  245. bool CAction::FindNPCByIcon(POINT ptMap, LPCTSTR lpszNPCName)
  246. {
  247. TCHAR szNPC[MAX_PATH] = { 0 };
  248. _stprintf_s(szNPC, _T("img\\npc\\%s.bmp"), lpszNPCName);
  249. CRect rc = FindMatchIcon(szNPC);
  250. if (rc.IsRectEmpty() || rc.IsRectNull())
  251. {
  252. WorldMapPositioning(ptMap);
  253. Sleep(600); // 有时走到目标点,NPC还未刷新出来;
  254. rc = FindMatchIcon(szNPC);
  255. if (rc.IsRectEmpty() || rc.IsRectNull())
  256. {
  257. // 可能还是未刷新出来,再次等待;
  258. Sleep(300);
  259. rc = FindMatchIcon(szNPC);
  260. if (rc.IsRectEmpty() || rc.IsRectNull())
  261. {
  262. return false;
  263. }
  264. }
  265. }
  266. // 找到目标,单击;
  267. MouseMove(CPoint(rc.left, rc.top));
  268. // 单击:我要挑战他们;
  269. MouseClick({ (rc.right + rc.left) / 2, (rc.top + rc.bottom) / 2 });
  270. SRAND(900, 1300);// 等对话框出现;
  271. // 移动鼠标:不要停留在人物身上;
  272. MouseMove({ rc.left - rand() % 10, rc.top - rand() % 10 });
  273. return true;
  274. }
  275. bool CAction::FindNPCByIconAndPos(LPCTSTR lpszNPCName, POINT ptMap, POINT ptNpc)
  276. {
  277. TCHAR szNPC[MAX_PATH] = { 0 };
  278. _stprintf_s(szNPC, _T("img\\npc\\%s.bmp"), lpszNPCName);
  279. CRect rc = FindMatchIcon(szNPC);
  280. if (rc.IsRectEmpty() || rc.IsRectNull())
  281. {
  282. WorldMapPositioning(ptMap);
  283. Sleep(600); // 有时走到目标点,NPC还未刷新出来;
  284. rc = FindMatchIcon(szNPC);
  285. if (rc.IsRectEmpty() || rc.IsRectNull())
  286. {
  287. // 可能还是未刷新出来,再次等待;
  288. Sleep(300);
  289. rc = FindMatchIcon(szNPC);
  290. if (rc.IsRectEmpty() || rc.IsRectNull())
  291. {
  292. return false;
  293. }
  294. }
  295. }
  296. // 找到目标,单击;
  297. MouseMove(CPoint(rc.left, rc.top));
  298. // 单击:我要挑战他们;
  299. MouseClick(ptNpc);
  300. // 遍历3次;
  301. bool bFind = false;
  302. for (int i = 0; i < 3; i++)
  303. {
  304. Sleep(800 - i * 150);
  305. CRect rc = FindMatchIcon("img\\对话框关闭按钮.bmp");
  306. if (!rc.IsRectEmpty() && !rc.IsRectNull())
  307. {
  308. bFind = true;
  309. break;
  310. }
  311. }
  312. // 移动鼠标:不要停留在人物身上;
  313. MouseMove({ ptNpc.x - rand() % 10, ptNpc.y - rand() % 10 });
  314. return bFind;
  315. }
  316. void CAction::BattleJY(BOOL bHasSummoner, int nZJType, BOOL bAggressiveSkill)
  317. {
  318. BOOL bFirstAttack = TRUE;
  319. while (IsBattle()) { // 是否在战斗中;
  320. // 是否我方攻击;
  321. while (IsWattingAttack(0)) { // Bug:如果被镜妖打死了,无法中断;
  322. DebugLog(_T("BattleJY:我方攻击……"));
  323. if (bFirstAttack)
  324. {
  325. SRAND(250, 300);
  326. DebugLog(_T("BattleJY:第一次攻击……"));
  327. // 根据角色选择技能;
  328. SendKey(VK_F1);
  329. SRAND(300, 350);
  330. if (bAggressiveSkill) // 打镜妖喽罗;
  331. {
  332. MouseClick(CPoint(290 - 3, 140 - 20));
  333. //GAssist::MouseMove(hWnd, CPoint(220 - 5, 180 - 10));// 打主镜妖;
  334. SRAND(350, 500);
  335. }
  336. if (bHasSummoner) {
  337. CRect rc;
  338. // 召唤兽技能;
  339. DebugLog(_T("BattleJY:召唤兽技能选择"));
  340. // 选择重击;
  341. if (nZJType) {
  342. SRAND(150, 350);
  343. SendKey(VK_S, FALSE, TRUE);
  344. DebugLog(_T("BattleJY:召唤兽技能选择:%d"), nZJType);
  345. if (ImgAssist::GetImgMatchtemplate(m_hGameWnd, nZJType == 1 ? GameGlobal::g_strAppdir + _T("img\\活动\\镜妖\\重击.bmp") : GameGlobal::g_strAppdir + _T("img\\活动\\镜妖\\重击2.bmp"), rc))
  346. {
  347. SRAND(150, 300);
  348. MouseClick(CPoint((rc.right + rc.left) / 2 + rand() % 3, (rc.top + rc.bottom) / 2 + rand() % 3));
  349. }
  350. }
  351. SRAND(300, 500);
  352. MouseMove(CPoint(290 - 3, 140 - 15));
  353. MouseClick(CPoint(290 - 3, 140 - 20));
  354. }
  355. bFirstAttack = FALSE;
  356. }
  357. else
  358. {
  359. DebugLog(_T("BattleJY:不是第一次攻击……"));
  360. SendKey(VK_A, FALSE, TRUE);
  361. SRAND(80, 100);
  362. if (bFirstAttack) {
  363. SendKey(VK_A, FALSE, TRUE);
  364. SRAND(80, 120);
  365. }
  366. }
  367. }
  368. SRAND(500, 850);
  369. }
  370. // 非战斗中,是否在贫民房中;
  371. if (!IsOnMap(_T("img\\地图\\贫民房.bmp"))) {
  372. // 找小米;
  373. CRect rc;
  374. if (!FindNPCByWorldMap("臭美的小米"))
  375. return;
  376. // 选择进入;
  377. BOOL bMatch = FALSE;
  378. int trySize = 15;
  379. // 我要挑战他们;
  380. while (!(bMatch = ClickDlgSetup(_T("img\\活动\\镜妖\\1-我帮您抓.bmp")))) {
  381. SRAND(800, 1200);
  382. if (!trySize--) break;
  383. }
  384. if (bMatch) {
  385. MouseMove(CPoint(rc.left, rc.top));
  386. // 单击:我要挑战他们;
  387. MouseClick(CPoint((rc.right + rc.left) / 2, (rc.top + rc.bottom) / 2));
  388. SRAND(1000, 1500);// 等出图;
  389. // 移动一下;
  390. MouseMove(CPoint(rc.left, rc.top));
  391. ::SetCursorPos(rc.left + rand() % 6, rc.top + rand() % 8);
  392. // 是否第一次进入地图;
  393. if (ClickDlgSetup(_T("img\\活动\\镜妖\\2-我这就去.bmp")))
  394. {
  395. SRAND(600, 1200);
  396. if (ClickDlgSetup(_T("img\\活动\\镜妖\\3-战意镜妖.bmp")))
  397. {
  398. SRAND(600, 1800);
  399. if ((bMatch = ClickDlgSetup(_T("img\\活动\\镜妖\\4-我就选择它.bmp"))))
  400. {
  401. SRAND(800, 1800);
  402. }
  403. }
  404. }
  405. }
  406. }
  407. else
  408. {
  409. // 找到镜妖;
  410. CRect rc;
  411. POINT spt[4] = { { 250, 335 }, { 310,360}, { 385, 315}, { 280,295 } };
  412. for (int i = 0; !ImgAssist::GetImgMatchtemplate(m_hGameWnd, GameGlobal::g_strAppdir + _T("img\\活动\\镜妖\\镜妖1.bmp"), rc); i++)
  413. {
  414. // 打开世界地图;
  415. SendKey(VK_TAB); SRAND(200, 300);
  416. // 选择坐标点;
  417. MouseClick(spt[i % 4]);
  418. // 退出世界地图;
  419. SendKey(VK_TAB); SRAND(500, 900);
  420. // 等待跑完成;
  421. //SRAND(600, 800);
  422. IsWalkStop();
  423. if (i > 10) {
  424. DebugLog(_T("未找到镜妖+10"));
  425. break;
  426. }
  427. }
  428. // 找到镜妖;
  429. MouseMove(CPoint(rc.left + rand() % 10, rc.top + rand() % 10));
  430. DebugLog(_T("移动鼠标"));
  431. MouseClick(CPoint((rc.right + rc.left) / 2 + rand() % 3, (rc.top + rc.bottom) / 2 + rand() % 3));
  432. SRAND(390, 600);
  433. // 单击目标后,要往移走鼠标,防止下面匹配的时候被鼠标挡住;
  434. MouseMove(CPoint(rc.right + RAND(10, 20), rc.bottom + RAND(10, 20)));
  435. SRAND(300, 500);
  436. DebugLog(_T("找到目标并单击成功:%s"));
  437. // 我来抓你;
  438. if (ImgAssist::GetImgMatchtemplate(m_hGameWnd, GameGlobal::g_strAppdir + _T("img\\活动\\镜妖\\我来抓您的.bmp"), rc)) {
  439. // 找到匹配的模块;
  440. SRAND(200, 300);
  441. //GAssist::MouseMove(hWnd, CPoint(rc.left + rand() % 10, rc.top + rand() % 10));
  442. DebugLog(_T("我来抓你:移动鼠标"));
  443. MouseClick(CPoint((rc.right + rc.left) / 2 + rand() % 3, (rc.top + rc.bottom) / 2 + rand() % 3));
  444. SRAND(1000, 1500);
  445. DebugLog(_T("我来抓你:找到目标并单击成功:%s"));
  446. }
  447. }
  448. }
  449. };