Basic Bot - ability to save and load bot files

This commit is contained in:
adelikat 2015-09-05 23:39:51 -04:00
parent b8295f1d98
commit 92a7773b3a
2 changed files with 287 additions and 3 deletions

View File

@ -42,6 +42,7 @@
this.countRerecordsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.countRerecordsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.RunBtn = new System.Windows.Forms.Button(); this.RunBtn = new System.Windows.Forms.Button();
this.BotStatusStrip = new System.Windows.Forms.StatusStrip(); this.BotStatusStrip = new System.Windows.Forms.StatusStrip();
this.MessageLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.ControlsBox = new System.Windows.Forms.GroupBox(); this.ControlsBox = new System.Windows.Forms.GroupBox();
this.ControlProbabilityPanel = new System.Windows.Forms.Panel(); this.ControlProbabilityPanel = new System.Windows.Forms.Panel();
this.BestGroupBox = new System.Windows.Forms.GroupBox(); this.BestGroupBox = new System.Windows.Forms.GroupBox();
@ -83,6 +84,7 @@
this.ClearBestButton = new System.Windows.Forms.Button(); this.ClearBestButton = new System.Windows.Forms.Button();
this.PlayBestButton = new System.Windows.Forms.Button(); this.PlayBestButton = new System.Windows.Forms.Button();
this.BotMenu.SuspendLayout(); this.BotMenu.SuspendLayout();
this.BotStatusStrip.SuspendLayout();
this.ControlsBox.SuspendLayout(); this.ControlsBox.SuspendLayout();
this.BestGroupBox.SuspendLayout(); this.BestGroupBox.SuspendLayout();
this.panel1.SuspendLayout(); this.panel1.SuspendLayout();
@ -115,6 +117,7 @@
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Text = "&File"; this.fileToolStripMenuItem.Text = "&File";
this.fileToolStripMenuItem.DropDownOpened += new System.EventHandler(this.FileSubMenu_DropDownOpened);
// //
// NewMenuItem // NewMenuItem
// //
@ -123,6 +126,7 @@
this.NewMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N))); this.NewMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
this.NewMenuItem.Size = new System.Drawing.Size(195, 22); this.NewMenuItem.Size = new System.Drawing.Size(195, 22);
this.NewMenuItem.Text = "&New"; this.NewMenuItem.Text = "&New";
this.NewMenuItem.Click += new System.EventHandler(this.NewMenuItem_Click);
// //
// OpenMenuItem // OpenMenuItem
// //
@ -131,6 +135,7 @@
this.OpenMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O))); this.OpenMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
this.OpenMenuItem.Size = new System.Drawing.Size(195, 22); this.OpenMenuItem.Size = new System.Drawing.Size(195, 22);
this.OpenMenuItem.Text = "&Open..."; this.OpenMenuItem.Text = "&Open...";
this.OpenMenuItem.Click += new System.EventHandler(this.OpenMenuItem_Click);
// //
// SaveMenuItem // SaveMenuItem
// //
@ -139,6 +144,7 @@
this.SaveMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S))); this.SaveMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
this.SaveMenuItem.Size = new System.Drawing.Size(195, 22); this.SaveMenuItem.Size = new System.Drawing.Size(195, 22);
this.SaveMenuItem.Text = "&Save"; this.SaveMenuItem.Text = "&Save";
this.SaveMenuItem.Click += new System.EventHandler(this.SaveMenuItem_Click);
// //
// SaveAsMenuItem // SaveAsMenuItem
// //
@ -147,6 +153,7 @@
| System.Windows.Forms.Keys.S))); | System.Windows.Forms.Keys.S)));
this.SaveAsMenuItem.Size = new System.Drawing.Size(195, 22); this.SaveAsMenuItem.Size = new System.Drawing.Size(195, 22);
this.SaveAsMenuItem.Text = "Save &As..."; this.SaveAsMenuItem.Text = "Save &As...";
this.SaveAsMenuItem.Click += new System.EventHandler(this.SaveAsMenuItem_Click);
// //
// RecentSubMenu // RecentSubMenu
// //
@ -156,11 +163,12 @@
this.RecentSubMenu.Name = "RecentSubMenu"; this.RecentSubMenu.Name = "RecentSubMenu";
this.RecentSubMenu.Size = new System.Drawing.Size(195, 22); this.RecentSubMenu.Size = new System.Drawing.Size(195, 22);
this.RecentSubMenu.Text = "Recent"; this.RecentSubMenu.Text = "Recent";
this.RecentSubMenu.DropDownOpened += new System.EventHandler(this.RecentSubMenu_DropDownOpened);
// //
// toolStripSeparator2 // toolStripSeparator2
// //
this.toolStripSeparator2.Name = "toolStripSeparator2"; this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(57, 6); this.toolStripSeparator2.Size = new System.Drawing.Size(149, 6);
// //
// toolStripSeparator1 // toolStripSeparator1
// //
@ -173,7 +181,7 @@
this.ExitMenuItem.ShortcutKeyDisplayString = "Alt+F4"; this.ExitMenuItem.ShortcutKeyDisplayString = "Alt+F4";
this.ExitMenuItem.Size = new System.Drawing.Size(195, 22); this.ExitMenuItem.Size = new System.Drawing.Size(195, 22);
this.ExitMenuItem.Text = "E&xit"; this.ExitMenuItem.Text = "E&xit";
this.ExitMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); this.ExitMenuItem.Click += new System.EventHandler(this.ExitMenuItem_Click);
// //
// optionsToolStripMenuItem // optionsToolStripMenuItem
// //
@ -203,12 +211,20 @@
// //
// BotStatusStrip // BotStatusStrip
// //
this.BotStatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.MessageLabel});
this.BotStatusStrip.Location = new System.Drawing.Point(0, 530); this.BotStatusStrip.Location = new System.Drawing.Point(0, 530);
this.BotStatusStrip.Name = "BotStatusStrip"; this.BotStatusStrip.Name = "BotStatusStrip";
this.BotStatusStrip.Size = new System.Drawing.Size(574, 22); this.BotStatusStrip.Size = new System.Drawing.Size(574, 22);
this.BotStatusStrip.TabIndex = 2; this.BotStatusStrip.TabIndex = 2;
this.BotStatusStrip.Text = "statusStrip1"; this.BotStatusStrip.Text = "statusStrip1";
// //
// MessageLabel
//
this.MessageLabel.Name = "MessageLabel";
this.MessageLabel.Size = new System.Drawing.Size(109, 17);
this.MessageLabel.Text = " ";
//
// ControlsBox // ControlsBox
// //
this.ControlsBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.ControlsBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
@ -667,6 +683,8 @@
this.Load += new System.EventHandler(this.BasicBot_Load); this.Load += new System.EventHandler(this.BasicBot_Load);
this.BotMenu.ResumeLayout(false); this.BotMenu.ResumeLayout(false);
this.BotMenu.PerformLayout(); this.BotMenu.PerformLayout();
this.BotStatusStrip.ResumeLayout(false);
this.BotStatusStrip.PerformLayout();
this.ControlsBox.ResumeLayout(false); this.ControlsBox.ResumeLayout(false);
this.BestGroupBox.ResumeLayout(false); this.BestGroupBox.ResumeLayout(false);
this.BestGroupBox.PerformLayout(); this.BestGroupBox.PerformLayout();
@ -736,5 +754,6 @@
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripMenuItem NewMenuItem; private System.Windows.Forms.ToolStripMenuItem NewMenuItem;
private System.Windows.Forms.Button PlayBestButton; private System.Windows.Forms.Button PlayBestButton;
private System.Windows.Forms.ToolStripStatusLabel MessageLabel;
} }
} }

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Client.EmuHawk.ToolExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using BizHawk.Client.Common; using BizHawk.Client.Common;
@ -13,6 +14,9 @@ namespace BizHawk.Client.EmuHawk
{ {
public partial class BasicBot : Form , IToolFormAutoConfig public partial class BasicBot : Form , IToolFormAutoConfig
{ {
private const string DialogTitle = "Basic Bot";
private string _currentFileName = string.Empty;
private bool _isBotting = false; private bool _isBotting = false;
private long _attempts = 1; private long _attempts = 1;
private long _frames = 0; private long _frames = 0;
@ -42,7 +46,12 @@ namespace BizHawk.Client.EmuHawk
public class BasicBotSettings public class BasicBotSettings
{ {
public BasicBotSettings()
{
RecentBotFiles = new RecentFiles();
}
public RecentFiles RecentBotFiles { get; set; }
} }
#endregion #endregion
@ -52,10 +61,17 @@ namespace BizHawk.Client.EmuHawk
public BasicBot() public BasicBot()
{ {
InitializeComponent(); InitializeComponent();
Text = DialogTitle;
Settings = new BasicBotSettings();
} }
private void BasicBot_Load(object sender, EventArgs e) private void BasicBot_Load(object sender, EventArgs e)
{ {
MaximizeAddressBox.SetHexProperties(MemoryDomains.MainMemory.Size);
TieBreaker1Box.SetHexProperties(MemoryDomains.MainMemory.Size);
TieBreaker2Box.SetHexProperties(MemoryDomains.MainMemory.Size);
TieBreaker3Box.SetHexProperties(MemoryDomains.MainMemory.Size);
StartFromSlotBox.SelectedIndex = 0; StartFromSlotBox.SelectedIndex = 0;
int starty = 0; int starty = 0;
@ -77,6 +93,11 @@ namespace BizHawk.Client.EmuHawk
accumulatedy += lineHeight; accumulatedy += lineHeight;
count++; count++;
} }
if (Settings.RecentBotFiles.AutoLoad)
{
LoadFileFromRecent(Settings.RecentBotFiles.MostRecent);
}
} }
#endregion #endregion
@ -128,6 +149,26 @@ namespace BizHawk.Client.EmuHawk
private int FrameLength private int FrameLength
{ {
get { return (int)FrameLengthNumeric.Value; } get { return (int)FrameLengthNumeric.Value; }
set { FrameLengthNumeric.Value = value; }
}
public int MaximizeAddress
{
get
{
int? addr = MaximizeAddressBox.ToRawInt();
if (addr.HasValue)
{
return addr.Value;
}
return 0;
}
set
{
MaximizeAddressBox.SetFromRawInt(value);
}
} }
public int MaximizeValue public int MaximizeValue
@ -144,6 +185,25 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public int TieBreaker1Address
{
get
{
int? addr = TieBreaker1Box.ToRawInt();
if (addr.HasValue)
{
return addr.Value;
}
return 0;
}
set
{
TieBreaker1Box.SetFromRawInt(value);
}
}
public int TieBreaker1Value public int TieBreaker1Value
{ {
get get
@ -158,6 +218,25 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public int TieBreaker2Address
{
get
{
int? addr = TieBreaker2Box.ToRawInt();
if (addr.HasValue)
{
return addr.Value;
}
return 0;
}
set
{
TieBreaker2Box.SetFromRawInt(value);
}
}
public int TieBreaker2Value public int TieBreaker2Value
{ {
get get
@ -172,6 +251,25 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public int TieBreaker3Address
{
get
{
int? addr = TieBreaker3Box.ToRawInt();
if (addr.HasValue)
{
return addr.Value;
}
return 0;
}
set
{
TieBreaker3Box.SetFromRawInt(value);
}
}
public int TieBreaker3Value public int TieBreaker3Value
{ {
get get
@ -186,6 +284,32 @@ namespace BizHawk.Client.EmuHawk
} }
} }
public string FromSlot
{
get
{
return StartFromSlotBox.SelectedItem != null
? StartFromSlotBox.SelectedItem.ToString()
: string.Empty;
}
set
{
var item = StartFromSlotBox.Items.
OfType<object>()
.FirstOrDefault(o => o.ToString() == value);
if (item != null)
{
StartFromSlotBox.SelectedItem = item;
}
else
{
StartFromSlotBox.SelectedItem = null;
}
}
}
#endregion #endregion
#region IToolForm Implementation #region IToolForm Implementation
@ -216,7 +340,12 @@ namespace BizHawk.Client.EmuHawk
#region Control Events #region Control Events
private void exitToolStripMenuItem_Click(object sender, EventArgs e) private void NewMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("TODO");
}
private void ExitMenuItem_Click(object sender, EventArgs e)
{ {
Close(); Close();
} }
@ -247,6 +376,57 @@ namespace BizHawk.Client.EmuHawk
GlobalWin.MainForm.UnpauseEmulator(); GlobalWin.MainForm.UnpauseEmulator();
} }
private void FileSubMenu_DropDownOpened(object sender, EventArgs e)
{
SaveMenuItem.Enabled = !string.IsNullOrWhiteSpace(_currentFileName);
}
private void RecentSubMenu_DropDownOpened(object sender, EventArgs e)
{
RecentSubMenu.DropDownItems.Clear();
RecentSubMenu.DropDownItems.AddRange(
Settings.RecentBotFiles.RecentMenu(LoadFileFromRecent, true));
}
private void OpenMenuItem_Click(object sender, EventArgs e)
{
var file = ToolHelpers.OpenFileDialog(
_currentFileName,
PathManager.GetRomsPath(Global.Game.System), // TODO: bot path
"Bot files",
"bot"
);
if (file != null)
{
LoadBotFile(file.FullName);
}
}
private void SaveMenuItem_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(_currentFileName))
{
SaveBotFile(_currentFileName);
}
}
private void SaveAsMenuItem_Click(object sender, EventArgs e)
{
var file = ToolHelpers.SaveFileDialog(
_currentFileName,
PathManager.GetRomsPath(Global.Game.System), // TODO: bot path
"Bot files",
"bot"
);
if (file != null)
{
SaveBotFile(file.FullName);
Text = DialogTitle + Path.GetFileNameWithoutExtension(_currentFileName);
}
}
#endregion #endregion
private class BotAttempt private class BotAttempt
@ -264,6 +444,18 @@ namespace BizHawk.Client.EmuHawk
public List<string> Log { get; set; } public List<string> Log { get; set; }
} }
private class BotData
{
public BotAttempt Best { get; set; }
public Dictionary<string, double> ControlProbabilities { get; set; }
public int Maximize { get; set; }
public int TieBreaker1 { get; set; }
public int TieBreaker2 { get; set; }
public int TieBreaker3 { get; set; }
public int FrameLength { get; set; }
public string FromSlot { get; set; }
}
private int GetRamvalue(int addr) private int GetRamvalue(int addr)
{ {
// TODO: ability to pick memory domain // TODO: ability to pick memory domain
@ -488,5 +680,78 @@ namespace BizHawk.Client.EmuHawk
GlobalWin.MainForm.PauseEmulator(); GlobalWin.MainForm.PauseEmulator();
} }
private void SaveBotFile(string path)
{
var data = new BotData
{
Best = _bestBotAttempt,
ControlProbabilities = ControlProbabilities,
Maximize = MaximizeAddress,
TieBreaker1 = TieBreaker1Address,
TieBreaker2 = TieBreaker2Address,
TieBreaker3 = TieBreaker3Address,
FromSlot = FromSlot
};
var json = ConfigService.SaveWithType(data);
File.WriteAllText(path, json);
_currentFileName = path;
Settings.RecentBotFiles.Add(_currentFileName);
MessageLabel.Text = Path.GetFileName(_currentFileName) + " saved";
}
private void LoadFileFromRecent(string path)
{
var result = LoadBotFile(path);
if (!result)
{
Settings.RecentBotFiles.HandleLoadError(path);
}
}
private bool LoadBotFile(string path)
{
var file = new FileInfo(path);
if (!file.Exists)
{
return false;
}
var json = File.ReadAllText(path);
var botData = (BotData)ConfigService.LoadWithType(json);
_bestBotAttempt = botData.Best;
var probabilityControls = ControlProbabilityPanel.Controls
.OfType<BotControlsRow>()
.ToList();
foreach (var kvp in botData.ControlProbabilities)
{
var control = probabilityControls.Single(c => c.ButtonName == kvp.Key);
control.Probability = kvp.Value;
}
MaximizeAddress = botData.Maximize;
TieBreaker1Address = botData.TieBreaker1;
TieBreaker2Address = botData.TieBreaker2;
TieBreaker3Address = botData.TieBreaker3;
FrameLength = botData.FrameLength;
FromSlot = botData.FromSlot;
UpdateBestAttempt();
if (_bestBotAttempt != null)
{
PlayBestButton.Enabled = true;
}
_currentFileName = path;
Settings.RecentBotFiles.Add(_currentFileName);
return true;
}
} }
} }