Skip to content

Commit

Permalink
Add item cheats to the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEqualsCoder committed Dec 31, 2024
1 parent a619a5e commit aa510cc
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 54 deletions.
22 changes: 22 additions & 0 deletions src/TrackerCouncil.Smz3.Abstractions/ITrackerModeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public interface ITrackerModeService
/// </summary>
public int PegsPegged { get; protected set; }

/// <summary>
/// If cheats are currently enabled
/// </summary>
public bool CheatsEnabled { get; protected set; }

// <summary>
/// Occurs when Peg World mode has been toggled on.
/// </summary>
Expand All @@ -49,6 +54,11 @@ public interface ITrackerModeService
/// </summary>
public event EventHandler<TrackerEventArgs>? GoModeToggledOff;

/// <summary>
/// Occurs when cheats are enabled or disabled
/// </summary>
public event EventHandler<TrackerEventArgs>? CheatsToggled;

/// <summary>
/// Toggles Go Mode on.
/// </summary>
Expand Down Expand Up @@ -86,4 +96,16 @@ public interface ITrackerModeService
/// </summary>
/// <param name="confidence">The speech recognition confidence.</param>
public void StopShaktoolMode(float? confidence = null);

/// <summary>
/// Enables cheats
/// </summary>
/// <param name="confidence">The speech recognition confidence.</param>
public void EnableCheats(float? confidence = null);

/// <summary>
/// Disables cheats
/// </summary>
/// <param name="confidence">The speech recognition confidence.</param>
public void DisableCheats(float? confidence = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public async Task<bool> TryGiveItemAsync(Item item, int? fromPlayerId)
/// <returns>False if it is currently unable to give an item to the player</returns>
public async Task<bool> TryGiveItemsAsync(List<Item> items, int fromPlayerId)
{
if (!IsInGame())
if (!IsInGame() || TrackerBase.World.Config.RomGenerator != RomGenerator.Cas)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ public class TextToSpeechCommunicator : ICommunicator, IDisposable
private DateTime _startSpeakingTime;
private ConcurrentQueue<SpeechRequest> _pendingRequests = [];
private SpeechRequest? _currentSpeechRequest;
private ILogger<ICommunicator> _logger;

/// <summary>
/// Initializes a new instance of the <see
/// cref="TextToSpeechCommunicator"/> class.
/// </summary>
public TextToSpeechCommunicator(TrackerOptionsAccessor trackerOptionsAccessor, ILogger<ICommunicator> logger)
{
_logger = logger;

if (!OperatingSystem.IsWindows())
{
return;
Expand Down Expand Up @@ -101,6 +104,8 @@ public void Abort()
/// </param>
public void Say(SpeechRequest request)
{
_logger.LogDebug($"Speech: {request.Text}");

if (!OperatingSystem.IsWindows() || !_canSpeak)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ internal class TrackerModeService : TrackerService, ITrackerModeService
public bool PegWorldMode { get; set; }
public bool ShaktoolMode { get; set; }
public int PegsPegged { get; set; }

public bool CheatsEnabled { get; set; }

public event EventHandler<TrackerEventArgs>? ToggledPegWorldModeOn;
public event EventHandler<TrackerEventArgs>? ToggledShaktoolMode;
public event EventHandler<TrackerEventArgs>? PegPegged;
public event EventHandler<TrackerEventArgs>? GoModeToggledOn;
public event EventHandler<TrackerEventArgs>? GoModeToggledOff;
public event EventHandler<TrackerEventArgs>? CheatsToggled;

public void ToggleGoMode(float? confidence = null)
{
Expand Down Expand Up @@ -137,4 +139,42 @@ public void StopShaktoolMode(float? confidence = null)
ToggledShaktoolMode?.Invoke(this, new TrackerEventArgs(confidence));
});
}

public void EnableCheats(float? confidence = null)
{
if (!CheatsEnabled)
{
CheatsEnabled = true;
Tracker.Say(x => x.Cheats.EnabledCheats);
CheatsToggled?.Invoke(this, new TrackerEventArgs(confidence));
AddUndo(() =>
{
CheatsEnabled = false;
ToggledShaktoolMode?.Invoke(this, new TrackerEventArgs(confidence));
});
}
else
{
Tracker.Say(x => x.Cheats.AlreadyEnabledCheats);
}
}

public void DisableCheats(float? confidence = null)
{
if (CheatsEnabled)
{
CheatsEnabled = false;
Tracker.Say(x => x.Cheats.DisabledCheats);
CheatsToggled?.Invoke(this, new TrackerEventArgs(confidence));
AddUndo(() =>
{
CheatsEnabled = true;
ToggledShaktoolMode?.Invoke(this, new TrackerEventArgs(confidence));
});
}
else
{
Tracker.Say(x => x.Cheats.AlreadyDisabledCheats);
}
}
}
48 changes: 12 additions & 36 deletions src/TrackerCouncil.Smz3.Tracking/VoiceCommands/CheatsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ namespace TrackerCouncil.Smz3.Tracking.VoiceCommands;
/// <summary>
/// Module for cheats via the auto tracker
/// </summary>
public class CheatsModule : TrackerModule
public class CheatsModule(
TrackerBase tracker,
IPlayerProgressionService playerProgressionService,
IWorldQueryService worldQueryService,
ITrackerModeService trackerModeService,
ILogger<CheatsModule> logger) : TrackerModule(tracker,
playerProgressionService,
worldQueryService,
logger)
{
private static readonly string s_fillCheatKey = "FillType";
private static readonly List<string> s_fillHealthChoices = new() { "health", "hp", "energy", "hearts" };
Expand All @@ -27,25 +35,9 @@ public class CheatsModule : TrackerModule
private static readonly List<string> s_fillSuperMissileChoices = new() { "super missiles", "soup" };
private static readonly List<string> s_fillPowerBombsChoices = new() { "power bombs", "hamburgers" };

private bool _cheatsEnabled;

/// <summary>
/// Initializes a new instance of the <see cref="AutoTrackerVoiceModule"/>
/// class.
/// </summary>
/// <param name="tracker">The tracker instance.</param>
/// <param name="playerProgressionService">Service to get item information</param>
/// <param name="worldQueryService">Service to get world information</param>
/// <param name="logger">Used to write logging information.</param>
public CheatsModule(TrackerBase tracker, IPlayerProgressionService playerProgressionService, IWorldQueryService worldQueryService, ILogger<CheatsModule> logger)
: base(tracker, playerProgressionService, worldQueryService, logger)
{

}

private bool PlayerCanCheat()
{
if (!_cheatsEnabled)
if (!trackerModeService.CheatsEnabled)
{
TrackerBase.Say(x => x.Cheats.PromptEnableCheats);
return false;
Expand Down Expand Up @@ -226,28 +218,12 @@ public override void AddCommands()

AddCommand("Enable cheats", GetEnableCheatsRule(), (result) =>
{
if (!_cheatsEnabled)
{
_cheatsEnabled = true;
TrackerBase.Say(x => x.Cheats.EnabledCheats);
}
else
{
TrackerBase.Say(x => x.Cheats.AlreadyEnabledCheats);
}
trackerModeService.EnableCheats();
});

AddCommand("Disable cheats", GetDisableHintsRule(), (result) =>
{
if (_cheatsEnabled)
{
_cheatsEnabled = false;
TrackerBase.Say(x => x.Cheats.DisabledCheats);
}
else
{
TrackerBase.Say(x => x.Cheats.AlreadyDisabledCheats);
}
trackerModeService.DisableCheats();
});

AddCommand("Fill rule", FillRule(), (result) =>
Expand Down
Binary file removed src/TrackerCouncil.Smz3.UI/Assets/check.png
Binary file not shown.
39 changes: 39 additions & 0 deletions src/TrackerCouncil.Smz3.UI/Services/TrackerWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class TrackerWindowService(
ITrackerTimerService trackerTimerService,
IServiceProvider serviceProvider,
IWorldQueryService worldQueryService,
IGameService gameService,

Check warning on line 39 in src/TrackerCouncil.Smz3.UI/Services/TrackerWindowService.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'gameService' is unread.

Check warning on line 39 in src/TrackerCouncil.Smz3.UI/Services/TrackerWindowService.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'gameService' is unread.
ILogger<TrackerWindowService> logger) : ControlService
{
private RandomizerOptions? _options;
Expand Down Expand Up @@ -114,6 +115,11 @@ public TrackerWindowViewModel GetViewModel(TrackerWindow parent)
ToggleShaktoolMode(tracker.ModeTracker.ShaktoolMode);
};

tracker.ModeTracker.CheatsToggled += (sender, args) =>
{
UpdateCheatOptions(tracker.ModeTracker.CheatsEnabled);
};

tracker.DisableVoiceRecognition();

if (TrackerWindowPanelViewModel.NumberImagePaths.Count == 0)
Expand Down Expand Up @@ -548,6 +554,17 @@ private TrackerWindowPanelViewModel GetItemPanelViewModel(UIGridLocation gridLoc
}
};

model.CheatItemRequested += async (_, args) =>
{
if (tracker.GameService == null)
{
return;
}

var item = args.Item;
await tracker.GameService.TryGiveItemAsync(item, null);
};

return model;
}

Expand Down Expand Up @@ -766,6 +783,16 @@ private RandomizerOptions Options
}
}

private void UpdateCheatOptions(bool cheatsEnabled)
{
_model.AreCheatsEnabled = cheatsEnabled;

foreach (var panel in _model.Panels)
{
panel.CheatsEnabled = cheatsEnabled;
}
}

public void ToggleSpeechRecognition()
{
if (tracker.VoiceRecognitionEnabled)
Expand All @@ -777,4 +804,16 @@ public void ToggleSpeechRecognition()
tracker.EnableVoiceRecognition();
}
}

public void ToggleCheats()
{
if (tracker.ModeTracker.CheatsEnabled)
{
tracker.ModeTracker.DisableCheats();
}
else
{
tracker.ModeTracker.EnableCheats();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class TrackerWindowItemPanelViewModel : TrackerWindowPanelViewModel

public event EventHandler<ItemDungeonRequirementEventArgs>? ItemSetAsDungeonRequirement;

public event EventHandler<ItemChangedEventArgs>? CheatItemRequested;

public void UpdateItem(Item? item, string? path)
{
if (Items != null && item != null && !string.IsNullOrEmpty(path))
Expand Down Expand Up @@ -146,7 +148,12 @@ public override List<MenuItem> GetMenuItems()
{
var menuItems = new List<MenuItem>();

if (Items?.Count > 1)
if (Items == null || Items.Count == 0)
{
return menuItems;
}

if (Items.Count > 1)
{
foreach (var item in Items.Keys)
{
Expand All @@ -164,7 +171,7 @@ public override List<MenuItem> GetMenuItems()
}
}
}
else if (Items?.Count == 1)
else if (Items.Count == 1)
{
var item = Items.Keys.First();
if (item.TrackingState > 0)
Expand All @@ -175,7 +182,7 @@ public override List<MenuItem> GetMenuItems()
}
}

if (Items?.Keys.FirstOrDefault()?.Type is ItemType.Bombos or ItemType.Ether or ItemType.Quake)
if (Items.Keys.FirstOrDefault()?.Type is ItemType.Bombos or ItemType.Ether or ItemType.Quake)
{
var item = Items.Keys.First();

Expand All @@ -200,6 +207,16 @@ public override List<MenuItem> GetMenuItems()
menuItems.Add(menuItem);
}

if (CheatsEnabled)
{
foreach (var item in Items.Keys)
{
var menuItem = new MenuItem { Header = $"Cheat: Give {item.Name}" };
menuItem.Click += (_, _) => CheatItemRequested?.Invoke(this, new ItemChangedEventArgs { Item = item });
menuItems.Add(menuItem);
}
}

return menuItems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class TrackerWindowPanelViewModel : ViewModelBase
public int Column { get; set; }
public int Row { get; set; }
public bool AddShadows { get; set; }
[Reactive] public bool CheatsEnabled { get; set; }

[Reactive] public List<TrackerWindowPanelImage?> Images { get; set; } = [];
[Reactive] public List<TrackerWindowPanelImage?> OverlayImages { get; set; } = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class TrackerWindowViewModel : ViewModelBase
[ReactiveLinkedProperties(nameof(IsDisabledConnector), nameof(IsSniConnector), nameof(IsUsb2SnesConnector), nameof(IsLuaConnector), nameof(IsLuaCrowdControlConnector), nameof(IsLuaEmoTrackerConnector))]
public SnesConnectorType SnesConnectorType { get; set; } = SnesConnectorType.None;

[Reactive] public bool AreCheatsEnabled { get; set; }

public bool IsDisabledConnector => SnesConnectorType == SnesConnectorType.None;
public bool IsSniConnector => SnesConnectorType == SnesConnectorType.Sni;
public bool IsUsb2SnesConnector => SnesConnectorType == SnesConnectorType.Usb2Snes;
Expand Down
Loading

0 comments on commit aa510cc

Please sign in to comment.