Skip to content

Commit

Permalink
Fully automated buyer function for buildings and upgrades!
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelCanovas committed Sep 7, 2021
1 parent 2b9e3b7 commit 2b30233
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 54 deletions.
7 changes: 5 additions & 2 deletions Cookie Clicker/MainWindow.xaml
Expand Up @@ -24,14 +24,14 @@
<SolidColorBrush Color="#FFAADAD6" Opacity="1"/>
</Button.Background>
</Button>
<TextBlock x:Name="Explication1" HorizontalAlignment="Left" Margin="290,0,0,125" Text="( Click Set and press a key )" TextWrapping="Wrap" VerticalAlignment="Bottom" FontSize="15" Padding="0,0,0,0"/>
<TextBlock x:Name="Explication1" HorizontalAlignment="Left" Margin="290,0,0,125" Text="( Click SET and press a key )" TextWrapping="Wrap" VerticalAlignment="Bottom" FontSize="15" Padding="0,0,0,0"/>
<TextBlock x:Name="Explication2" HorizontalAlignment="Left" Margin="150,0,0,70" Text="( milliseconds before Start )" TextWrapping="Wrap" VerticalAlignment="Bottom" FontSize="14"/>
<TextBlock x:Name="Explication3" HorizontalAlignment="Left" Margin="225,0,0,30" Text="( milliseconds )" TextWrapping="Wrap" VerticalAlignment="Bottom" FontSize="14"/>
<TextBlock x:Name="AuthorTittle" HorizontalAlignment="Right" Margin="0,0,180,60" Text="Author:" TextWrapping="Wrap" VerticalAlignment="Bottom" FontSize="18" FontWeight="Bold"/>
<TextBlock x:Name="AuthorName" HorizontalAlignment="Right" Margin="0,0,50,64" Text="Angel Canovas" TextWrapping="Wrap" VerticalAlignment="Bottom"/>
<TextBlock x:Name="GitlabTittle" HorizontalAlignment="Right" Margin="0,0,180,25" Text="Gitlab:" TextWrapping="Wrap" VerticalAlignment="Bottom" FontWeight="Bold" FontSize="16"/>
<TextBlock HorizontalAlignment="Right" Margin="0,0,50,27" Text="@AngelCanovas" TextWrapping="Wrap" VerticalAlignment="Bottom" FontSize="11"/>
<TextBlock x:Name="versionTextBox" HorizontalAlignment="Left" Margin="670,25,0,0" Text="V 1.5" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="22" FontWeight="Bold" FontFamily="Segoe UI Black"/>
<TextBlock x:Name="versionTextBox" HorizontalAlignment="Left" Margin="670,25,0,0" Text="V 1.6" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="22" FontWeight="Bold" FontFamily="Segoe UI Black"/>
<TextBlock x:Name="xPositionLabel" HorizontalAlignment="Left" Margin="315,90,0,0" Text="(Pixels) Width:" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14" RenderTransformOrigin="-0.12,0.557"/>
<CheckBox x:Name="fixPositionCheckBox" Content="Fix position for Golden cookie" HorizontalAlignment="Left" Margin="50,90,0,0" VerticalAlignment="Top" FontSize="16" FontWeight="Bold" Checked="checkFixPosition" Unchecked="uncheckFixPosition"/>
<TextBox x:Name="xPositionTextBox" Text="{Binding XPosition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" PreviewTextInput="NumberValidationTextBox" HorizontalAlignment="Left" Margin="410,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="0.502,0.868" MinWidth="50" FontSize="14"/>
Expand All @@ -48,5 +48,8 @@
<TextBlock x:Name="goldenWarningTextBlock" HorizontalAlignment="Left" Margin="240,210,0,0" Text="* For it to work mute all the buildings before *" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14" TextDecorations="{x:Null}" RenderTransformOrigin="-0.002,1.171"/>
<TextBox x:Name="goldenScanTextBox" Text="{Binding GoldenScanDelay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="700,210,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14" MinHeight="10" MinWidth="50" PreviewTextInput="NumberValidationTextBox"/>
<TextBlock x:Name="goldenScanTextBlock" HorizontalAlignment="Left" Margin="550,210,0,0" Text="Do it every (seconds)" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14"/>
<CheckBox x:Name="automaticModeCheckBox" Content="Buy upgrades automatically!" HorizontalAlignment="Left" Margin="50,260,0,0" VerticalAlignment="Top" FontSize="16" FontWeight="Bold" Checked="checkAutomaticMode" Unchecked="uncheckAutomaticMode"/>
<TextBlock x:Name="automaticModeTextBlock" HorizontalAlignment="Left" Margin="320,260,0,0" Text="Do it every ( seconds )" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14"/>
<TextBox x:Name="automaticModeTextBox" Text="{Binding AutomaticModeDelay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="500,260,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="14" MinHeight="10" MinWidth="50" PreviewTextInput="NumberValidationTextBox"/>
</Grid>
</Window>
214 changes: 162 additions & 52 deletions Cookie Clicker/MainWindow.xaml.cs
Expand Up @@ -24,6 +24,8 @@ public partial class MainWindow : Window
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;
Expand All @@ -38,6 +40,7 @@ public partial class MainWindow : Window
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)
Expand All @@ -47,6 +50,7 @@ public partial class MainWindow : Window
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;
Expand Down Expand Up @@ -90,6 +94,8 @@ public MainWindow()
IsBarrelRollCheck = false;
IsGoldenScanCheck = false;
GoldenScanDelay = 30;
IsAutomaticModeCheck = false;
AutomaticModeDelay = 10;
}

protected override void OnSourceInitialized(EventArgs e)
Expand Down Expand Up @@ -132,59 +138,75 @@ private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref
else if (vkey == KeyInterop.VirtualKeyFromKey(toggleKey))
{
// Key Press Logic
if (canAutoClickerStart)
if (toggleAutoClickerState)
{
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)
{
// if clicker status is running (true), toggle to false to stop it
if (dispatcherTimer != null)
{
dispatcherTimer.Stop();
}
if (goldenDispatcherTimer != null)
{
goldenDispatcherTimer.Stop();
}

Stop();
// 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();
}
else

if (IsAutomaticModeCheck)
{
// 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.Interval = new TimeSpan(0, 0, 1);
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();
}
// 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();
}
}
}
Expand Down Expand Up @@ -227,7 +249,13 @@ 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()
Expand Down Expand Up @@ -258,6 +286,15 @@ public static void LeftMouseClick(int xpos, int ypos)
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)
{
Expand Down Expand Up @@ -342,12 +379,14 @@ private void doABarrelRoll(object sender, RoutedEventArgs e)

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);
}
Expand All @@ -359,15 +398,17 @@ private void doAGoldenScan(object sender, RoutedEventArgs e)
{
Point originalMousePosition = GetMousePosition();
Point goldenStartPoint = new Point(5, 180);
Point goldenEndPoint = new Point(1570, 1000);
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 (x < 100 && y > 900) { continue; } // avoid Klumbor in the left bottom side
//if (!toggleAutoClickerState) { break; }
if (x < 90 && y > 890) { continue; } // avoid Klumbor in the left bottom side
Thread.Sleep(5);
LeftMouseClick(x, y);
}
Expand All @@ -376,6 +417,65 @@ private void doAGoldenScan(object sender, RoutedEventArgs e)
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;
Expand All @@ -395,5 +495,15 @@ 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;
}
}
}

0 comments on commit 2b30233

Please sign in to comment.