Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REG-2180] Add monkey bot action space analysis results #353

Merged
merged 8 commits into from
Nov 14, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ public abstract class RGActionInput
/// <summary>
/// Simulate the input onto the game.
/// </summary>
public abstract void Perform();
/// <param name="segmentNumber">The bot segmentNumber</param>
public abstract void Perform(int segmentNumber);

/// <summary>
/// Returns whether this input affects the same part of the device as another.
///
///
/// This is used to decide whether two actions can be performed simultaneously:
/// if two actions affect the same part of the input device then they should NOT be performed together.
/// </summary>
Expand Down Expand Up @@ -50,16 +51,16 @@ public class LegacyKeyInput : RGActionInput
{
public readonly KeyCode KeyCode;
public readonly bool IsPressed;

public LegacyKeyInput(KeyCode keyCode, bool isPressed)
{
KeyCode = keyCode;
IsPressed = isPressed;
}
public override void Perform()

public override void Perform(int segmentNumber)
{
RGActionManager.SimulateKeyState(KeyCode, IsPressed);
RGActionManager.SimulateKeyState(segmentNumber, KeyCode, IsPressed);
}

public override bool Overlaps(RGActionInput other)
Expand Down Expand Up @@ -97,17 +98,17 @@ public InputSystemKeyInput(Key key, bool isPressed)
Key = key;
IsPressed = isPressed;
}
public override void Perform()

public override void Perform(int segmentNumber)
{
RGActionManager.SimulateKeyState(Key, IsPressed);
RGActionManager.SimulateKeyState(segmentNumber, Key, IsPressed);
}

public override bool Overlaps(RGActionInput other)
{
if (other is LegacyKeyInput keyInput)
{
return !(keyInput.KeyCode >= KeyCode.Mouse0 && keyInput.KeyCode <= KeyCode.Mouse6)
return !(keyInput.KeyCode >= KeyCode.Mouse0 && keyInput.KeyCode <= KeyCode.Mouse6)
&& Key == RGLegacyInputUtils.KeyCodeToInputSystemKey(keyInput.KeyCode);
} else if (other is InputSystemKeyInput inputSysKeyInput)
{
Expand All @@ -133,10 +134,10 @@ public MousePositionInput(Vector2 mousePosition)
{
MousePosition = mousePosition;
}
public override void Perform()

public override void Perform(int segmentNumber)
{
RGActionManager.SimulateMouseMovement(MousePosition);
RGActionManager.SimulateMouseMovement(segmentNumber, MousePosition);
}

public override bool Overlaps(RGActionInput other)
Expand All @@ -158,10 +159,10 @@ public MousePositionDeltaInput(Vector2 mousePositionDelta)
{
MousePositionDelta = mousePositionDelta;
}
public override void Perform()

public override void Perform(int segmentNumber)
{
RGActionManager.SimulateMouseMovementDelta(MousePositionDelta);
RGActionManager.SimulateMouseMovementDelta(segmentNumber, MousePositionDelta);
}

public override bool Overlaps(RGActionInput other)
Expand All @@ -183,10 +184,10 @@ public MouseScrollInput(Vector2 mouseScroll)
{
MouseScroll = mouseScroll;
}
public override void Perform()

public override void Perform(int segmentNumber)
{
RGActionManager.SimulateMouseScroll(MouseScroll);
RGActionManager.SimulateMouseScroll(segmentNumber, MouseScroll);
}

public override bool Overlaps(RGActionInput other)
Expand All @@ -207,7 +208,7 @@ public class MouseButtonInput : RGActionInput
public const int MIDDLE_MOUSE_BUTTON = 2;
public const int FORWARD_MOUSE_BUTTON = 3;
public const int BACK_MOUSE_BUTTON = 4;

public readonly int MouseButton;
public readonly bool IsPressed;

Expand All @@ -216,10 +217,10 @@ public MouseButtonInput(int mouseButton, bool isPressed)
MouseButton = mouseButton;
IsPressed = isPressed;
}
public override void Perform()

public override void Perform(int segmentNumber)
{
RGActionManager.SimulateMouseButton(MouseButton, IsPressed);
RGActionManager.SimulateMouseButton(segmentNumber, MouseButton, IsPressed);
}

public override bool Overlaps(RGActionInput other)
Expand All @@ -243,4 +244,4 @@ public override string ToString()
return "MouseButtonInput(" + MouseButton + ", " + IsPressed + ")";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ private static bool DoesContextNeedSetUp()
/// <summary>
/// Start an action manager session. This should be called prior to any calls to GetValidActions().
/// </summary>
/// <param name="segmentNumber">The bot sequence segment number</param>
/// <param name="context">The MonoBehaviour context under which actions will be simulated.</param>
/// <param name="actionSettings">Session-specific action settings (optional - if null will use saved configuration)</param>
public static void StartSession(MonoBehaviour context, RGActionManagerSettings actionSettings = null)
public static void StartSession(int segmentNumber, MonoBehaviour context, RGActionManagerSettings actionSettings = null)
{
if (_context != null)
{
Expand Down Expand Up @@ -175,7 +176,9 @@ public static void StartSession(MonoBehaviour context, RGActionManagerSettings a
RGUtils.ConfigureInputSettings();
}

InitInputState();
InitInputState(segmentNumber);

RGActionRuntimeCoverageAnalysis.StartRecording(segmentNumber);
}

/// <summary>
Expand All @@ -197,6 +200,8 @@ public static void StopSession()
}
_context = null;
_settings = LoadSettings(); // restore settings back to the saved configuration

RGActionRuntimeCoverageAnalysis.StopRecording();
}
}

Expand Down Expand Up @@ -228,12 +233,12 @@ public static IEnumerable<IRGGameActionInstance> GetValidActions()

foreach (var obj in objects)
{
if (obj is Component c && !c.gameObject.activeInHierarchy)
if (obj is Component { gameObject: { activeInHierarchy: false } })
{
// skip components that are on inactive game objects
continue;
}
if (obj is Behaviour b && !b.isActiveAndEnabled)
if (obj is Behaviour { isActiveAndEnabled: false })
{
// skip disabled behaviours
continue;
Expand Down Expand Up @@ -288,10 +293,10 @@ private static void OnSceneLoad(Scene s, LoadSceneMode m)
public static Dictionary<long, ObjectStatus> CurrentTransforms { get; private set; }
public static List<EventSystem> CurrentEventSystems { get; private set; }

private static void InitInputState()
private static void InitInputState(int segmentNumber)
{
// move mouse off screen
_mousePosition = MouseEventSender.MoveMouseOffScreen();
_mousePosition = MouseEventSender.MoveMouseOffScreen(segmentNumber);

#if ENABLE_LEGACY_INPUT_MANAGER
_mouseScroll = RGLegacyInputWrapper.mouseScrollDelta;
Expand All @@ -312,7 +317,7 @@ private static void InitInputState()
CurrentEventSystems = new List<EventSystem>();
}

public static void SimulateKeyState(KeyCode keyCode, bool isPressed)
public static void SimulateKeyState(int segmentNumber, KeyCode keyCode, bool isPressed)
{
if (keyCode >= KeyCode.Mouse0 && keyCode <= KeyCode.Mouse6)
{
Expand All @@ -323,7 +328,7 @@ public static void SimulateKeyState(KeyCode keyCode, bool isPressed)
keyCode == KeyCode.Mouse3 ? isPressed : _forwardMouseButton;
bool backButton =
keyCode == KeyCode.Mouse4 ? isPressed : _backMouseButton;
MouseEventSender.SendRawPositionMouseEvent(0,
MouseEventSender.SendRawPositionMouseEvent(segmentNumber,
_mousePosition,
leftButton: leftButton, middleButton: middleButton, rightButton: rightButton,
forwardButton: forwardButton, backButton: backButton, scroll: _mouseScroll);
Expand All @@ -336,43 +341,43 @@ public static void SimulateKeyState(KeyCode keyCode, bool isPressed)
else
{
Key key = RGLegacyInputUtils.KeyCodeToInputSystemKey(keyCode);
SimulateKeyState(key, isPressed);
SimulateKeyState(segmentNumber, key, isPressed);
}
}

public static void SimulateKeyState(Key key, bool isPressed)
public static void SimulateKeyState(int segmentNumber, Key key, bool isPressed)
{
if (key != Key.None)
{
KeyboardEventSender.SendKeyEvent(0, key, isPressed ? KeyState.Down : KeyState.Up);
KeyboardEventSender.SendKeyEvent(segmentNumber, key, isPressed ? KeyState.Down : KeyState.Up);
}
}

public static void SimulateMouseMovement(Vector2 mousePosition)
public static void SimulateMouseMovement(int segmentNumber, Vector2 mousePosition)
{
MouseEventSender.SendRawPositionMouseEvent(0, mousePosition, leftButton: _leftMouseButton, middleButton: _middleMouseButton,
MouseEventSender.SendRawPositionMouseEvent(segmentNumber, mousePosition, leftButton: _leftMouseButton, middleButton: _middleMouseButton,
rightButton: _rightMouseButton, forwardButton: _forwardMouseButton, backButton: _backMouseButton, scroll: _mouseScroll);
_mousePosition = mousePosition;
}

public static void SimulateMouseMovementDelta(Vector2 mousePositionDelta)
public static void SimulateMouseMovementDelta(int segmentNumber, Vector2 mousePositionDelta)
{
Vector3 delta = mousePositionDelta;
SimulateMouseMovement(_mousePosition + delta);
SimulateMouseMovement(segmentNumber, _mousePosition + delta);
}

public static void SimulateMouseScroll(Vector2 mouseScroll)
public static void SimulateMouseScroll(int segmentNumber, Vector2 mouseScroll)
{
MouseEventSender.SendRawPositionMouseEvent(0, _mousePosition, leftButton: _leftMouseButton,
MouseEventSender.SendRawPositionMouseEvent(segmentNumber, _mousePosition, leftButton: _leftMouseButton,
middleButton: _middleMouseButton,
rightButton: _rightMouseButton, forwardButton: _forwardMouseButton, backButton: _backMouseButton,
scroll: mouseScroll);
_mouseScroll = mouseScroll;
}

public static void SimulateMouseButton(int mouseButton, bool isPressed)
public static void SimulateMouseButton(int segmentNumber, int mouseButton, bool isPressed)
{
SimulateKeyState(KeyCode.Mouse0 + mouseButton, isPressed);
SimulateKeyState(segmentNumber, KeyCode.Mouse0 + mouseButton, isPressed);
}
}
}
Loading
Loading