Skip to content

Commit

Permalink
refactoring, started adding InputService, outdated config detection i…
Browse files Browse the repository at this point in the history
…mprovement
  • Loading branch information
TalicZealot committed Apr 29, 2021
1 parent 00dd2d7 commit 4f0576f
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 128 deletions.
1 change: 1 addition & 0 deletions SotnRandoTools/src/Coop/CoopSender.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using BizHawk.Client.Common;
using SotnApi.Constants.Values.Alucard;
Expand Down
1 change: 1 addition & 0 deletions SotnRandoTools/src/CoopForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
Expand Down
191 changes: 73 additions & 118 deletions SotnRandoTools/src/RandoTracker/Tracker.cs

Large diffs are not rendered by default.

212 changes: 212 additions & 0 deletions SotnRandoTools/src/Services/InputService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Client.Common;
using SotnApi.Interfaces;
using SotnRandoTools.Services.Models;

namespace SotnRandoTools.Services
{
public class InputService
{
private readonly IJoypadApi joypadApi;
private readonly IAlucardApi alucardApi;
private List<Dictionary<string, object>> inputHistory = new();
private List<Dictionary<string, bool>> moveHistory = new();
private Input dragonPunch = new Input {
MotionSequence = new List<Dictionary<string, object>>
{
new Dictionary<string, object> {["P1 Forward"] = true, ["P1 Down"] = false},
new Dictionary<string, object> {["P1 Forward"] = false, ["P1 Down"] = true},
new Dictionary<string, object> {["P1 Forward"] = true, ["P1 Down"] = true}
},
Activator = new Dictionary<string, object> {["P1 L2"] = true }
};
private Input halfCircle = new Input
{
MotionSequence = new List<Dictionary<string, object>>
{
new Dictionary<string, object> {["P1 Back"] = true, ["P1 Down"] = false},
new Dictionary<string, object> {["P1 Forward"] = true, ["P1 Down"] = true},
new Dictionary<string, object> {["P1 Forward"] = false, ["P1 Down"] = true},
new Dictionary<string, object> {["P1 Back"] = true, ["P1 Down"] = true},
new Dictionary<string, object> {["P1 Forward"] = true, ["P1 Down"] = false}
},
Activator = new Dictionary<string, object> { ["P1 L2"] = true }
};

public InputService(IJoypadApi joypadApi, IAlucardApi alucardApi)
{
if (joypadApi is null) throw new ArgumentNullException(nameof(joypadApi));
if (alucardApi is null) throw new ArgumentNullException(nameof(alucardApi));
this.joypadApi = joypadApi;
this.alucardApi = alucardApi;
}

public void UpdateInputs()
{
inputHistory.Add(joypadApi.Get().ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
if (inputHistory.Count > 120)
{
inputHistory.RemoveAt(0);
}

if (alucardApi.FacingLeft)
{
inputHistory[inputHistory.Count - 1].Add("P1 Forward", Convert.ToBoolean(inputHistory[inputHistory.Count - 1]["P1 Left"]));
inputHistory[inputHistory.Count - 1].Add("P1 Back", Convert.ToBoolean(inputHistory[inputHistory.Count - 1]["P1 Right"]));
}
else
{
inputHistory[inputHistory.Count - 1].Add("P1 Forward", Convert.ToBoolean(inputHistory[inputHistory.Count - 1]["P1 Right"]));
inputHistory[inputHistory.Count - 1].Add("P1 Back", Convert.ToBoolean(inputHistory[inputHistory.Count - 1]["P1 Left"]));
}

moveHistory.Add(new Dictionary<string, bool>());
if (ReadInput(dragonPunch, 30))
{
moveHistory[moveHistory.Count - 1].Add("DP", true);
}
else
{
moveHistory[moveHistory.Count - 1].Add("DP", false);
}

if (ReadInput(halfCircle, 30))
{
moveHistory[moveHistory.Count - 1].Add("HCF", true);
}
else
{
moveHistory[moveHistory.Count - 1].Add("HCF", false);
}

if (moveHistory.Count > 120)
{
moveHistory.RemoveAt(0);
}
}

public bool RegisteredDp
{
get
{
for (int i = 0; i < 11; i++)
{
if (i >= moveHistory.Count)
{
return false;
}

if (moveHistory[i]["DP"] == true)
{
return true;
}
}
return false;
}
}

private bool ReadInput(Input moveInput, uint bufferSize)
{
if (moveInput is null) throw new ArgumentNullException(nameof(moveInput));
if (bufferSize < 1) throw new ArgumentOutOfRangeException(nameof(bufferSize));

int inputIndex = 0;
int inputBuffer = 0;
bool keyUp = true;
bool directionalInput = false;
bool pressed = true;

var requiredActivator = moveInput.Activator.Where(pair => Convert.ToBoolean(pair.Value)).ToArray();
foreach (var pair in requiredActivator)
{
if (inputHistory.Count > 0 && !Convert.ToBoolean(inputHistory[inputHistory.Count - 1][pair.Key]))
{
return false;
}
else if (inputHistory.Count == 0)
{
return false;
}
}

for (int i = 0; i < inputHistory.Count; i++)
{
pressed = true;

if (inputBuffer > bufferSize)
{
return false;
}

if (!keyUp && inputIndex > 0)
{
bool buttonHeld = true;
var previousInputRequiredDirections = moveInput.MotionSequence[inputIndex - 1].Where(pair => Convert.ToBoolean(pair.Value)).ToArray();
foreach (var pair in previousInputRequiredDirections)
{
if (!Convert.ToBoolean(inputHistory[i][pair.Key]))
{
keyUp = true;
buttonHeld = false;
break;
}
}
if (buttonHeld)
{
inputBuffer++;
}
}
else if (keyUp && inputIndex > 0)
{
inputBuffer++;
}

if (!directionalInput)
{
foreach (var pair in moveInput.MotionSequence[inputIndex])
{
if (Convert.ToBoolean(inputHistory[i][pair.Key]) != Convert.ToBoolean(pair.Value))
{
pressed = false;
keyUp = true;
break;
}
}
}

if (pressed && !directionalInput)
{
if (inputIndex == moveInput.MotionSequence.Count - 1)
{
directionalInput = true;
inputBuffer = 0;
}
inputIndex++;
inputBuffer = 0;
}

pressed = true;

if (directionalInput && i < inputHistory.Count - 1)
{
foreach (var pair in requiredActivator)
{
if (!Convert.ToBoolean(inputHistory[i][pair.Key]))
{
pressed = false;
break;
}
}
if (pressed)
{
return false;
}
}
}

return directionalInput;
}
}
}
10 changes: 10 additions & 0 deletions SotnRandoTools/src/Services/Models/Input.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace SotnRandoTools.Services.Models
{
public class Input
{
public List<Dictionary<string, object>> MotionSequence { get; set; }
public Dictionary<string, object> Activator { get; set; }
}
}
2 changes: 1 addition & 1 deletion SotnRandoTools/src/SotnRandoTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<OutputType>Library</OutputType>
<EnforceCodeStyleInBuild>false</EnforceCodeStyleInBuild>
<AnalysisLevel>5.0</AnalysisLevel>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<Description>A collection of tools to enhance the experience of playign the SotN randomizer.</Description>
<Copyright />
</PropertyGroup>
Expand Down
24 changes: 15 additions & 9 deletions SotnRandoTools/src/ToolMainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk;
Expand All @@ -12,6 +13,7 @@
using SotnRandoTools.Configuration;
using SotnRandoTools.Constants;
using SotnRandoTools.Services;
using SotnRandoTools.Utils;

namespace SotnRandoTools
{
Expand Down Expand Up @@ -68,6 +70,7 @@ public partial class ToolMainForm : ToolFormBase, IExternalToolForm
private ToolConfig toolConfig;
private WatchlistService? watchlistService;
private NotificationService? notificationService;
private InputService? inputService;
private TrackerForm? trackerForm;
private KhaosForm? khaosForm;
private CoopForm? coopForm;
Expand All @@ -79,7 +82,6 @@ public partial class ToolMainForm : ToolFormBase, IExternalToolForm
private const int PanelOffset = 130;
private const int UpdateCooldownFrames = 10;
private int cooldown = 0;
private List<IDictionary<string, object>> inputHistory = new();
public ToolMainForm()
{
InitializeComponent();
Expand Down Expand Up @@ -107,6 +109,16 @@ private void InitializeConfig()
{
toolConfig.Khaos.Actions = defaultKhaosConfig.Actions;
}

var duplicateActions = toolConfig.Khaos.Actions.GroupBy(x => x.Name)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();

if (duplicateActions.Count > 0)
{
toolConfig.Khaos.Actions = defaultKhaosConfig.Actions;
}
}

protected override string WindowTitle => _windowTitle;
Expand Down Expand Up @@ -157,6 +169,7 @@ private void ToolMainForm_Load(object sender, EventArgs e)
gameApi = new GameApi(_maybeMemAPI);
renderingApi = new RenderingApi(_maybeMemAPI);
watchlistService = new WatchlistService(_memoryDomains, _emu?.SystemId, GlobalConfig);
inputService = new InputService(_maybeJoypadApi, alucardApi);
}

public override bool AskSaveChanges() => true;
Expand All @@ -165,15 +178,8 @@ public override void Restart() { }

public override void UpdateValues(ToolFormUpdateType type)
{
inputService.UpdateInputs();
cooldown++;
if (khaosForm is not null || coopForm is not null)
{
inputHistory.Add(_maybeJoypadApi.Get());
if (inputHistory.Count > 120)
{
inputHistory.RemoveAt(0);
}
}
if (cooldown == UpdateCooldownFrames)
{
cooldown = 0;
Expand Down

0 comments on commit 4f0576f

Please sign in to comment.