Skip to content

Commit

Permalink
Add support for Corporate Clash.
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
kpreisser committed Jan 23, 2022
1 parent 232b743 commit e8cb4a8
Show file tree
Hide file tree
Showing 76 changed files with 809 additions and 276 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Mouse Click Simulator for Toontown Rewritten
# Mouse Click Simulator for Toontown Rewritten and Corporate Clash

This is a new implementation of the older **"TT-Mausklick"** simulator, intended to work with Toontown Rewritten. It is implemented
in C# (.NET 6) and runs on Windows.
This is a new implementation of the older **"TT-Mausklick"** simulator, intended to work with Toontown Rewritten and Corporate Clash.
It is implemented in C# (.NET 6) and runs on Windows.

The TTR Mouse Click Simulator is able to automatically fish in specific locations like Punchling Place, TTC. To accomplish this, it scans the
The TT Mouse Click Simulator is able to automatically fish in specific locations like Punchling Place, TTC. To accomplish this, it scans the
screen to detect fish bubbles, calculates how far the rod must be cast to catch the fish, and moves the Toon to the fisherman to sell the fish.

Additionally, the simulator can plant and water flowers using 1 to 8 jellybean combinations, which is implemented using "Quick Actions".
Additionally, for Toontown Rewritten, the simulator can plant and water flowers using 1 to 8 jellybean combinations, which is implemented using "Quick Actions".
By clicking on a button with the flower's name, the Mouse Click Simulator will plant the flower by selecting the correct jellybean combination, and then water it.

You can watch a video of the <a href="https://www.youtube.com/watch?v=uq7VaJkO6-k" target="_blank">Automatic Fishing Function for Tenor Terrace</a>
and <a href="https://www.youtube.com/watch?v=dS-gBcvsjz4" target="_blank">Punchline Place</a>.

![](https://user-images.githubusercontent.com/13289184/148388183-a2010232-dec5-4d50-9893-0d9994b6ac17.png)

Note: This Simulator does not inject code into or otherwise manipulate the game. It only interacts with TTR by taking screenshots to analyze the window content
Note: This Simulator does not inject code into or otherwise manipulate the game. It only interacts with TT by taking screenshots to analyze the window content
(for the fishing action) and simulating mouse clicks/movements and pressing keys.

When enabling **Background Mode**, the simulator directly sends mouse and keyboard inputs to the Toontown window (instead of simulating gobal inputs),
so you can do other work while the simulator is running.

## WARNING
Use this program at your own risk!
Toontown Rewritten states in their Terms of Use that you should not use automation software, so you might risk a ban if you use this program.
Toontown Rewritten and Corporate Clash state in their Terms of Use that you should not use automation software, so you might risk a ban if you use this program.

## Running the Mouse Click Simulator

Expand Down Expand Up @@ -84,16 +84,18 @@ in the XML files, and in the source code calling `IInteractionProvider.MoveMouse
interpreted for a window with an inner (client area) size of **1600 × 1151** using a 4:3 aspect ratio. These values were kept from the
legacy TT mouse click simulator.

To specify mouse coordinates for Toontown Rewritten, you can do the following to get the resulting coordinates that can be used in the simulator:
- In the Toontown Options, set the display resolution to **800 × 600**. Do **not** resize the window after applying this resolution.
To specify mouse coordinates for Toontown, you can do the following to get the resulting coordinates that can be used in the simulator:
- In the Toontown Options, for **Toontown Rewritten**, set the display resolution to **800 × 600**. Do **not** resize the window after applying this resolution.
Or, for **Corporate Clash**, set the display resolution to any resolution you prefer, and set the aspect ratio to **4:3**.
- Determine the (x, y) coordinates within the client area of the window (that is, without the window borders and title bar), e.g. by
taking a screenshot with F9.
- Calculate the resulting coordinates as follows:
- Calculate the resulting coordinates as follows (replace 800 and 600 with the actual display resolution that you have set before):
- x<sub>result</sub> = x ÷ 800 × 1600
- y<sub>result</sub> = y ÷ 600 × 1151

In the C# code, you have then also specify the horizontal alignment (which is needed when the current window aspect ratio is greater than 4:3).
To determine this, resize your Toontown window to **increase the width** (or decrease the height).
To determine this, for **Toontown Rewritten**, resize your Toontown window to **increase the width** (or decrease the height).
Or, for **Corporate Clash**, reset the aspect ratio to **Adaptive**.
- If the element that your coordinates point to stays at the center of the window, specify `HorizontalScaleAlignment.Center`, or omit this parameter.
- Otherwise, if the element stays at the left hand side of the window, specify `HorizontalScaleAlignment.Left`.
- Otherwise, if the element stays at the right hand side of the window, specify `HorizontalScaleAlignment.Right`.
Expand Down
4 changes: 2 additions & 2 deletions TTMouseclickSimulator/App.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Application x:Class="TTMouseclickSimulator.App"
<Application x:Class="TTMouseClickSimulator.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TTMouseclickSimulator"
xmlns:local="clr-namespace:TTMouseClickSimulator"
StartupUri="MainWindow.xaml">
<Application.Resources>

Expand Down
2 changes: 1 addition & 1 deletion TTMouseclickSimulator/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Windows;

namespace TTMouseclickSimulator;
namespace TTMouseClickSimulator;

public partial class App : Application
{
Expand Down
4 changes: 2 additions & 2 deletions TTMouseclickSimulator/Core/Actions/AbstractAction.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

using TTMouseclickSimulator.Core.Environment;
using TTMouseClickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions;
namespace TTMouseClickSimulator.Core.Actions;

public abstract class AbstractAction : IAction
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace TTMouseclickSimulator.Core.Actions;
namespace TTMouseClickSimulator.Core.Actions;

public abstract class AbstractActionContainer : AbstractAction, IActionContainer
{
Expand Down
4 changes: 2 additions & 2 deletions TTMouseclickSimulator/Core/Actions/CompoundAction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;

using TTMouseclickSimulator.Core.Environment;
using TTMouseClickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions;
namespace TTMouseClickSimulator.Core.Actions;

/// <summary>
/// An action that loops through a list of other actions either in order or by chance.
Expand Down
4 changes: 2 additions & 2 deletions TTMouseclickSimulator/Core/Actions/IAction.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;

using TTMouseclickSimulator.Core.Environment;
using TTMouseClickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions;
namespace TTMouseClickSimulator.Core.Actions;

/// <summary>
/// An action is called by the simulator to do something, e.g. press keys or do mouse clicks.
Expand Down
2 changes: 1 addition & 1 deletion TTMouseclickSimulator/Core/Actions/IActionContainer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace TTMouseclickSimulator.Core.Actions;
namespace TTMouseClickSimulator.Core.Actions;

public interface IActionContainer : IAction
{
Expand Down
4 changes: 2 additions & 2 deletions TTMouseclickSimulator/Core/Actions/LoopAction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;

using TTMouseclickSimulator.Core.Environment;
using TTMouseClickSimulator.Core.Environment;

namespace TTMouseclickSimulator.Core.Actions;
namespace TTMouseClickSimulator.Core.Actions;

/// <summary>
/// Represents an action that runs another action in a loop.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Threading;

namespace TTMouseclickSimulator.Core.Environment;
namespace TTMouseClickSimulator.Core.Environment;

/// <summary>
/// Allows actions to interact with the target window, e.g. press keys and
Expand Down Expand Up @@ -77,9 +77,9 @@ public interface IInteractionProvider

void ReleaseMouseButton();

void PressKey(AbstractWindowsEnvironment.VirtualKey key);
void PressKey(WindowsEnvironment.VirtualKey key);

void ReleaseKey(AbstractWindowsEnvironment.VirtualKey key);
void ReleaseKey(WindowsEnvironment.VirtualKey key);

/// <summary>
/// Writes the given string into the window.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Windows.Media;

namespace TTMouseclickSimulator.Core.Environment;
namespace TTMouseClickSimulator.Core.Environment;

public interface IScreenshotContent
{
Expand Down
22 changes: 13 additions & 9 deletions TTMouseclickSimulator/Core/Environment/InteractionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
using System.Runtime.ExceptionServices;
using System.Threading;

namespace TTMouseclickSimulator.Core.Environment;
namespace TTMouseClickSimulator.Core.Environment;

internal class InteractionProvider : IInteractionProvider, IDisposable
public abstract class InteractionProvider : IInteractionProvider, IDisposable
{
protected readonly WindowsEnvironment environmentInterface;

private readonly bool backgroundMode;

private readonly CancellationTokenSource cancellationTokenSource = new();
private readonly SemaphoreSlim waitSemaphore = new(0);

private readonly Simulator simulator;
private readonly AbstractWindowsEnvironment environmentInterface;

private readonly List<AbstractWindowsEnvironment.VirtualKey> keysCurrentlyPressed = new();
private readonly List<WindowsEnvironment.VirtualKey> keysCurrentlyPressed = new();

private bool disposed;
private IntPtr windowHandle;

private AbstractWindowsEnvironment.ScreenshotContent? currentScreenshot;
private WindowsEnvironment.ScreenshotContent? currentScreenshot;

private bool isMouseButtonPressed;

Expand All @@ -35,7 +36,7 @@ internal class InteractionProvider : IInteractionProvider, IDisposable

public InteractionProvider(
Simulator simulator,
AbstractWindowsEnvironment environmentInterface,
WindowsEnvironment environmentInterface,
bool backgroundMode)
{
this.simulator = simulator;
Expand Down Expand Up @@ -78,7 +79,8 @@ public void Initialize()
{
// First, find the game processes. This will always return at least one process,
// or throw.
var processes = this.environmentInterface.FindProcesses();
var processes = this.FindProcesses();

try
{
if (processes.Count is 1)
Expand Down Expand Up @@ -307,7 +309,7 @@ public IScreenshotContent GetCurrentWindowScreenshot()
return this.GetCurrentWindowScreenshot(false);
}

public void PressKey(AbstractWindowsEnvironment.VirtualKey key)
public void PressKey(WindowsEnvironment.VirtualKey key)
{
this.ThrowIfCapabilityNotSet(SimulatorCapabilities.KeyboardInput);
this.CancellationToken.ThrowIfCancellationRequested();
Expand All @@ -326,7 +328,7 @@ public void PressKey(AbstractWindowsEnvironment.VirtualKey key)
}
}

public void ReleaseKey(AbstractWindowsEnvironment.VirtualKey key)
public void ReleaseKey(WindowsEnvironment.VirtualKey key)
{
this.ThrowIfCapabilityNotSet(SimulatorCapabilities.KeyboardInput);
this.CancellationToken.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -533,6 +535,8 @@ public void CancelActiveInteractions()
}
}

protected abstract List<Process> FindProcesses();

private void ThrowIfCapabilityNotSet(SimulatorCapabilities capabilities)
{
if (!this.simulator.RequiredCapabilities.IsSet(capabilities))
Expand Down
4 changes: 2 additions & 2 deletions TTMouseclickSimulator/Core/Environment/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace TTMouseclickSimulator.Core.Environment;
namespace TTMouseClickSimulator.Core.Environment;

internal static class NativeMethods
{
Expand Down Expand Up @@ -176,7 +176,7 @@ internal struct MOUSEINPUT
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT
{
internal AbstractWindowsEnvironment.VirtualKey wVk;
internal WindowsEnvironment.VirtualKey wVk;
internal ushort wScan;
internal KEYEVENTF dwFlags;
internal uint time;
Expand Down
2 changes: 1 addition & 1 deletion TTMouseclickSimulator/Core/Environment/WindowParameters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TTMouseclickSimulator.Core.Environment;
namespace TTMouseClickSimulator.Core.Environment;

/// <summary>
/// Specifies parameters of the destination window in pixels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace TTMouseclickSimulator.Core.Environment;
namespace TTMouseClickSimulator.Core.Environment;

/// <summary>
/// Provides methods to interact with other processes and windows on Windows.
/// </summary>
public abstract class AbstractWindowsEnvironment
public class WindowsEnvironment
{
private WindowsEnvironment()
{
}

public static WindowsEnvironment Instance
{
get;
} = new WindowsEnvironment();

/// <summary>
/// Finds processes with the specified name (without ".exe").
/// </summary>
/// <param name="processname"></param>
/// <returns></returns>
protected static List<Process> FindProcessesByName(string processname)
public List<Process> FindProcessesByName(string processname)
{
var processes = Process.GetProcessesByName(processname);
var foundProcesses = new List<Process>();
Expand All @@ -46,8 +55,6 @@ protected static List<Process> FindProcessesByName(string processname)
return foundProcesses;
}

public abstract List<Process> FindProcesses();

/// <summary>
/// Finds the main window of the given process and returns its window handle.
/// </summary>
Expand Down
15 changes: 10 additions & 5 deletions TTMouseclickSimulator/Core/Simulator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;

using TTMouseclickSimulator.Core.Actions;
using TTMouseclickSimulator.Core.Environment;
using TTMouseClickSimulator.Core.Actions;
using TTMouseClickSimulator.Core.Environment;
using TTMouseClickSimulator.Core.Toontown;
using TTMouseClickSimulator.Core.Toontown.Environment;

namespace TTMouseclickSimulator.Core;
namespace TTMouseClickSimulator.Core;

public class Simulator : IDisposable
{
Expand All @@ -16,8 +18,9 @@ public class Simulator : IDisposable
public event Action<bool?>? SimulatorInitializing;

public Simulator(
ToontownFlavor toontownFlavor,
IAction mainAction,
AbstractWindowsEnvironment environmentInterface,
WindowsEnvironment environmentInterface,
bool backgroundMode)
{
if (mainAction is null)
Expand All @@ -28,8 +31,10 @@ public Simulator(
this.mainAction = mainAction;
this.RequiredCapabilities = mainAction.RequiredCapabilities;

this.provider = new InteractionProvider(
// TODO: The simulator shouldn't have to know about this TT-specific subclass.
this.provider = new ToontownInteractionProvider(
this,
toontownFlavor,
environmentInterface,
backgroundMode);
}
Expand Down
2 changes: 1 addition & 1 deletion TTMouseclickSimulator/Core/SimulatorCapabilities.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace TTMouseclickSimulator.Core
namespace TTMouseClickSimulator.Core
{
[Flags]
public enum SimulatorCapabilities
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TTMouseclickSimulator.Core
namespace TTMouseClickSimulator.Core
{
public static class SimulatorCapabilitiesExtensions
{
Expand Down
11 changes: 9 additions & 2 deletions TTMouseclickSimulator/Core/SimulatorConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using System.Collections.Generic;

using TTMouseclickSimulator.Core.Actions;
using TTMouseClickSimulator.Core.Actions;
using TTMouseClickSimulator.Core.Toontown;

namespace TTMouseclickSimulator.Core;
namespace TTMouseClickSimulator.Core;

public class SimulatorConfiguration
{
public ToontownFlavor ToontownFlavor
{
get;
set;
}

/// <summary>
/// Specifies the action that is run by the simulator.
/// </summary>
Expand Down
Loading

0 comments on commit e8cb4a8

Please sign in to comment.