Skip to content

Commit

Permalink
Feature: configurable max gameplay arc angle
Browse files Browse the repository at this point in the history
  • Loading branch information
mkostoevr committed May 31, 2020
1 parent fad34ad commit 0d22cba
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 9 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Feature: configurable max gameplay arc angle
8 changes: 8 additions & 0 deletions include/cflf.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define FILE_LINE "(" __FILE__ ": " STR(__LINE__) ")"
#define DEBUG(...) { CHAR msg[512]; \
sprintf(msg, __VA_ARGS__); \
SetWindowTextA(userInterface.cStatusLabel.hHandle, msg);}
#define DEBUGMB(...) { CHAR msg[512]; \
sprintf(msg, __VA_ARGS__); \
MessageBoxA(NULL, msg, NULL, 0);}

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <commctrl.h>

#include <cflftypes.h>
#include <cflffuncs.h>
Expand Down
1 change: 1 addition & 0 deletions include/cflffuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FUNDEC VOID InitializeBot(PBOT pBot);
FUNDEC VOID DeinitializeBot(PBOT pBot);
FUNDEC VOID SwitchBotRunningState(PBOT pBot);
FUNDEC VOID SetBotSleepTime(PBOT pBot, DWORD dwSleepTime);
FUNDEC VOID SetBotCatchingAngle(PBOT pBot, DWORD dwCatchingAngle);

// cflfcfg.c
FUNDEC BOOL SaveConfigDword(LPCSTR szName, DWORD dwValue);
Expand Down
3 changes: 3 additions & 0 deletions include/cflftypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef struct tagBOT {
DWORD dwThreadId;
HANDLE hThread;
DWORD dwSleepTime;
DWORD dwCatchingAngle;
POINT pGameplayArcCatchPointOffset; // relative to GameplayArcVertex coordinates of point where to catch
} BOT, *PBOT;

typedef struct tagPROCESS {
Expand Down Expand Up @@ -47,4 +49,5 @@ typedef struct tagUI {
CONTROL cStatusLabel;
CONTROL cSleepTimeEdit;
CONTROL cSleepTimeLabel;
CONTROL cAngleTrackbar;
} UI, *PUI;
50 changes: 42 additions & 8 deletions src/cflfbot.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ static VOID FindKeyPoints(PBOT pBot, UINT x, UINT y) {
y += 241;
pBot->pGameplayArcVertex.x = x;
pBot->pGameplayArcVertex.y = y;
pBot->nGameplayArcRadius = 136;
y += pBot->nGameplayArcRadius;
pBot->pGameplayArcCenter.x = x;
pBot->pGameplayArcCenter.y = y;
Expand Down Expand Up @@ -97,35 +96,33 @@ static VOID FindGame(PBOT pBot) {
static DWORD WINAPI BotCicle(LPVOID lpParam) {
PBOT pBot;
LONG nEscPressed;
BOOL bItsTimeToPlay;

pBot = lpParam;
nEscPressed = 0;
bItsTimeToPlay = FALSE;
pBot->bGameIsFound = FALSE;
while (pBot->bIsWorking) {
// without this condition the bot would try to play when its own window is in focus
// so it would frequently press SPACE key, what activates last pressed button's click event
// it looks ugly when after bot activating the button starts frequently click itself
if (GetForegroundWindow() != userInterface.cMainWindow.hHandle) {
const COLORREF cGameplayArcVertexGreen = 0x34b133;
const COLORREF cGameplayArcVertexBlue = 0x3d6387;
const COLORREF cPlusButtonCenterUsual = 0x614707;
COLORREF cGameplayArcVertex;
COLORREF cPlusButtonCenter;

if (UpdateBotBitmap(pBot) == FALSE) {
Error(FILE_LINE);
}
cGameplayArcVertex = GetBitmapPixel(pBot->fbmp, pBot->pGameplayArcVertex.x, pBot->pGameplayArcVertex.y);
if (cGameplayArcVertex == cGameplayArcVertexGreen) {
nEscPressed = 0;
keybd_event(VK_SPACE, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
} else {
keybd_event(VK_SPACE, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
}
// if plus center color is unusual, highly likely this is cause some window openned
// (catched fish information, championship results, etc.)
// anyway, each window in the game may be closed pressing ESC key
cPlusButtonCenter = GetBitmapPixel(pBot->fbmp, pBot->pPlusButtonCenter.x, pBot->pPlusButtonCenter.y);
if (cPlusButtonCenter != cPlusButtonCenterUsual) {
// no usual color = no gameplay arc = no reason to play
bItsTimeToPlay = FALSE;
// if bot won't sleep here, before and between keyboard emulation, it may be detected
SleepBot(pBot);
keybd_event(VK_ESCAPE, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
Expand All @@ -144,6 +141,28 @@ static DWORD WINAPI BotCicle(LPVOID lpParam) {
nEscPressed = 0;
}
}
} else {
// if color of gameplay arc's vertex is blue or green, that means that the minigame started
cGameplayArcVertex = GetBitmapPixel(pBot->fbmp, pBot->pGameplayArcVertex.x, pBot->pGameplayArcVertex.y);
if (cGameplayArcVertex == cGameplayArcVertexBlue ||
cGameplayArcVertex == cGameplayArcVertexGreen) {
bItsTimeToPlay = TRUE;
} else {
// try to start the minigame if it isn't started
keybd_event(VK_SPACE, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
}
}
if (bItsTimeToPlay) {
cGameplayArcVertex = GetBitmapPixel(pBot->fbmp,
pBot->pGameplayArcVertex.x + pBot->pGameplayArcCatchPointOffset.x,
pBot->pGameplayArcCenter.y + pBot->pGameplayArcCatchPointOffset.y);
if (cGameplayArcVertex == cGameplayArcVertexBlue) {
keybd_event(VK_SPACE, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
} else {
nEscPressed = 0;
// XXX: possible detection - more ups than downs
keybd_event(VK_SPACE, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
SleepBot(pBot);
Expand Down Expand Up @@ -179,13 +198,19 @@ VOID InitializeBot(PBOT pBot) {
if (!LoadConfigDword("dwSleepTime", &pBot->dwSleepTime, 250)) {
Error(FILE_LINE);
}
if (!LoadConfigDword("dwCatchingAngle", &pBot->dwCatchingAngle, 90)) {
Error(FILE_LINE);
}
pBot->nGameplayArcRadius = 136;
SetBotCatchingAngle(pBot, pBot->dwCatchingAngle);
pBot->bIsWorking = FALSE;
pBot->bGameIsFound = FALSE;
pBot->fbmp.hHandle = NULL;
}

VOID DeinitializeBot(PBOT pBot) {
SaveConfigDword("dwSleepTime", pBot->dwSleepTime);
SaveConfigDword("dwCatchingAngle", pBot->dwCatchingAngle);
}

VOID SwitchBotRunningState(PBOT pBot) {
Expand All @@ -202,3 +227,12 @@ VOID SwitchBotRunningState(PBOT pBot) {
VOID SetBotSleepTime(PBOT pBot, DWORD dwSleepTime) {
pBot->dwSleepTime = dwSleepTime;
}

VOID SetBotCatchingAngle(PBOT pBot, DWORD dwCatchingAngle) {
DOUBLE fCatchingAngle;

fCatchingAngle = dwCatchingAngle * 3.14 / 180.0;
pBot->dwCatchingAngle = dwCatchingAngle;
pBot->pGameplayArcCatchPointOffset.x = pBot->nGameplayArcRadius * cos(fCatchingAngle) * -1.0;
pBot->pGameplayArcCatchPointOffset.y = pBot->nGameplayArcRadius * sin(fCatchingAngle) * -1.0;
}
28 changes: 28 additions & 0 deletions src/cflfui.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define HMENU_BOTCONTROLBUTTON ((HMENU)43)
#define HMENU_SPEEDEDIT ((HMENU)44)
#define HMENU_ANGLETRACKBAR ((HMENU)45)

VARDEF UI userInterface;

Expand Down Expand Up @@ -134,18 +135,37 @@ static VOID ConfigureSleepTimeLabel(PCONTROL pcSpeedLabel) {
pcSpeedLabel->lpParam = NULL;
}

static VOID ConfigureAngleTrackbar(PCONTROL pcAngleTrackbar) {
pcAngleTrackbar->dwExStyle = 0;
pcAngleTrackbar->lpClassName = TRACKBAR_CLASS;
pcAngleTrackbar->lpWindowName = NULL;
pcAngleTrackbar->dwStyle = WS_VISIBLE | WS_CHILD | TBS_HORZ;
pcAngleTrackbar->X = 0;
pcAngleTrackbar->Y = 101;
pcAngleTrackbar->nWidth = 250;
pcAngleTrackbar->nHeight = 21;
pcAngleTrackbar->hWndParent = userInterface.cMainWindow.hHandle;
pcAngleTrackbar->hMenu = HMENU_ANGLETRACKBAR;
pcAngleTrackbar->hInstance = userInterface.cMainWindow.hInstance;
pcAngleTrackbar->lpParam = NULL;
}

static VOID ConfigureInterface(PUI pUserInterface) {
ConfigureBotControlButton(&pUserInterface->cBotControlButton);
ConfigureStatusLabel(&pUserInterface->cStatusLabel);
ConfigureSleepTimeEdit(&pUserInterface->cSleepTimeEdit);
ConfigureSleepTimeLabel(&pUserInterface->cSleepTimeLabel);
ConfigureAngleTrackbar(&pUserInterface->cAngleTrackbar);
}

static VOID CreateInterface(PUI pUserInterface) {
CreateControl(&pUserInterface->cBotControlButton);
CreateControl(&pUserInterface->cStatusLabel);
CreateControl(&pUserInterface->cSleepTimeEdit);
CreateControl(&pUserInterface->cSleepTimeLabel);
CreateControl(&pUserInterface->cAngleTrackbar);
SendMessageA(pUserInterface->cAngleTrackbar.hHandle, TBM_SETRANGE, FALSE, MAKELPARAM(30, 150));
SendMessageA(pUserInterface->cAngleTrackbar.hHandle, TBM_SETPOS, TRUE, bot.dwCatchingAngle);
}

static VOID ShiftStringLeft(LPSTR szValue) {
Expand Down Expand Up @@ -195,6 +215,14 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
ConfigureInterface(&userInterface);
CreateInterface(&userInterface);
return 0;
case WM_HSCROLL:
if (lParam == (LPARAM)userInterface.cAngleTrackbar.hHandle) {
DWORD dwAngle;

dwAngle = SendMessageA(userInterface.cAngleTrackbar.hHandle, TBM_GETPOS, 0, 0);
SetBotCatchingAngle(&bot, dwAngle);
}
return 0;
case WM_COMMAND:
if (LOWORD(wParam) == (DWORD)userInterface.cBotControlButton.hMenu) {
SwitchBotRunningState(&bot);
Expand Down

0 comments on commit 0d22cba

Please sign in to comment.