Skip to content

Commit

Permalink
Added more object distance variables
Browse files Browse the repository at this point in the history
- Distance to home
- Mario distance to home
- Lateral distances of the above
  • Loading branch information
danebouchie committed Nov 6, 2016
1 parent 766e060 commit a27c2e2
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 142 deletions.
3 changes: 3 additions & 0 deletions Source/SM64 Diagnostic/Config/Config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<CoordinateOffsetX>0xA0</CoordinateOffsetX>
<CoordinateOffsetY>0xA4</CoordinateOffsetY>
<CoordinateOffsetZ>0xA8</CoordinateOffsetZ>
<HomeOffsetX>0x164</HomeOffsetX>
<HomeOffsetY>0x168</HomeOffsetY>
<HomeOffsetZ>0x16C</HomeOffsetZ>
<RotationOffset>0xC8</RotationOffset>
<MaxObjectSlots>240</MaxObjectSlots>
<MoveToMarioYOffset>300</MoveToMarioYOffset>
Expand Down
8 changes: 6 additions & 2 deletions Source/SM64 Diagnostic/Config/ObjectData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<Data type="uint" address="0x114" objectOffset="true" isAngle="true">Pitch (Rot Speed)</Data>
<Data type="uint" address="0x11c" objectOffset="true" isAngle="true">Roll (Rot Speed)</Data>
<Data type="float" address="0x015C" objectOffset="true">Dist to Mario</Data>
<Data special="true" specialType="DistanceToMario">Act. Dist to Mario</Data>
<Data special="true" specialType="LateralDistanceToMario">Lat. Dist to Mario</Data>
<Data special="true" specialType="MarioDistanceToObject">Act. Dist to Mario</Data>
<Data special="true" specialType="MarioLateralDistanceToObject">Lat. Dist to Mario</Data>
<Data type="uint" address="0x0160" objectOffset="true" isAngle="true">Angle to Mario</Data>
<Data type="float" address="0x019C" objectOffset="true">Drawing Dist</Data>
<Data special="true" specialType="RngCallsPerFrame">RNG Calls/Frame</Data>
Expand All @@ -37,6 +37,10 @@
<Data type="float" address="0x164" objectOffset="true">Home X</Data>
<Data type="float" address="0x168" objectOffset="true">Home Y</Data>
<Data type="float" address="0x16C" objectOffset="true">Home Z</Data>
<Data special="true" specialType="MarioDistanceToObjectHome">Mario Dist to Home</Data>
<Data special="true" specialType="MarioLateralDistanceToObjectHome">Mario Lat. Dist to Home</Data>
<Data special="true" specialType="ObjectDistanceToHome">Dist to Home</Data>
<Data special="true" specialType="LateralObjectDistanceToHome">Lat. Dist to Home</Data>
<Data type="uint" address="0x1CC" objectOffset="true" useHex="true">Release Status</Data>
<Data type="ushort" address="0x136" objectOffset="true" useHex="true">Interaction Status</Data>
<Data type="int" address="0x144" objectOffset="true">Subtype</Data>
Expand Down
47 changes: 47 additions & 0 deletions Source/SM64 Diagnostic/Extensions/ProcessStreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using SM64_Diagnostic.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SM64_Diagnostic.Extensions
{
public static class ProcessStreamExtensions
{
public static byte GetByte(this ProcessStream stream, uint address, bool absoluteAddress = false)
{
return stream.ReadRam(address, 1, absoluteAddress)[0];
}

public static sbyte GetSByte(this ProcessStream stream, uint address, bool absoluteAddress = false)
{
return (sbyte)stream.ReadRam(address, 1, absoluteAddress)[0];
}

public static short GetInt16(this ProcessStream stream, uint address, bool absoluteAddress = false)
{
return BitConverter.ToInt16(stream.ReadRam(address, 2, absoluteAddress), 0);
}

public static ushort GetUInt16(this ProcessStream stream, uint address, bool absoluteAddress = false)
{
return BitConverter.ToUInt16(stream.ReadRam(address, 2, absoluteAddress), 0);
}

public static int GetInt32(this ProcessStream stream, uint address, bool absoluteAddress = false)
{
return BitConverter.ToInt32(stream.ReadRam(address, 4, absoluteAddress), 0);
}

public static uint GetUInt32(this ProcessStream stream, uint address, bool absoluteAddress = false)
{
return BitConverter.ToUInt32(stream.ReadRam(address, 4, absoluteAddress), 0);
}

public static float GetSingle(this ProcessStream stream, uint address, bool absoluteAddress = false)
{
return BitConverter.ToSingle(stream.ReadRam(address, 4, absoluteAddress), 0);
}
}
}
2 changes: 1 addition & 1 deletion Source/SM64 Diagnostic/Managers/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CameraManager(ProcessStream stream, List<WatchVariable> cameraData, Contr
_cameraDataControls = new List<WatchVariableControl>();
foreach (WatchVariable watchVar in cameraData)
{
WatchVariableControl watchControl = new WatchVariableControl(_stream, watchVar, Config.Mario.MarioStructAddress);
WatchVariableControl watchControl = new WatchVariableControl(_stream, watchVar, Config.Mario.StructAddress);
variableTable.Controls.Add(watchControl.Control);
_cameraDataControls.Add(watchControl);
}
Expand Down
18 changes: 9 additions & 9 deletions Source/SM64 Diagnostic/Managers/MarioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public MarioManager(ProcessStream stream, List<WatchVariable> marioData, Control
{
if (!watchVar.Special)
{
WatchVariableControl watchControl = new WatchVariableControl(_stream, watchVar, Config.Mario.MarioStructAddress);
WatchVariableControl watchControl = new WatchVariableControl(_stream, watchVar, Config.Mario.StructAddress);
variableTable.Controls.Add(watchControl.Control);
_marioDataControls.Add(watchControl);
continue;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void Update(bool updateView)
{
// Get Mario position and rotation
float x, y, z, rot;
var marioAddress = Config.Mario.MarioStructAddress;
var marioAddress = Config.Mario.StructAddress;
x = BitConverter.ToSingle(_stream.ReadRam(marioAddress + Config.Mario.XOffset, 4), 0);
y = BitConverter.ToSingle(_stream.ReadRam(marioAddress + Config.Mario.YOffset, 4), 0);
z = BitConverter.ToSingle(_stream.ReadRam(marioAddress + Config.Mario.ZOffset, 4), 0);
Expand Down Expand Up @@ -116,7 +116,7 @@ public void Update(bool updateView)
cameraRot = (float)(((UInt16)(BitConverter.ToUInt32(_stream.ReadRam(Config.CameraRot, 4), 0))) / 65536f * 360f);

// Update floor triangle
UInt32 floorTriangle = BitConverter.ToUInt32(_stream.ReadRam(Config.Mario.MarioStructAddress + Config.Mario.FloorTriangleOffset, 4), 0);
UInt32 floorTriangle = BitConverter.ToUInt32(_stream.ReadRam(Config.Mario.StructAddress + Config.Mario.FloorTriangleOffset, 4), 0);
if (floorTriangle != 0x00)
{
Int16 x1 = BitConverter.ToInt16(_stream.ReadRam(floorTriangle + Config.TriangleOffsets.X1, 2), 0);
Expand Down Expand Up @@ -152,23 +152,23 @@ public void Update(bool updateView)
if (!updateView)
return;

var floorY = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.MarioStructAddress + Config.Mario.GroundYOffset, 4), 0);
var floorY = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.StructAddress + Config.Mario.GroundYOffset, 4), 0);

if (floorTriangle != 0x00)
{
float hSpeed = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.MarioStructAddress + Config.Mario.HSpeedOffset, 4), 0);
float hSpeed = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.StructAddress + Config.Mario.HSpeedOffset, 4), 0);
float normY = BitConverter.ToSingle(_stream.ReadRam(floorTriangle + Config.TriangleOffsets.NormY, 4), 0);
_deFactoSpeed.Text = (hSpeed * normY).ToString();
}
else
_deFactoSpeed.Text = "(No Floor)";

float slidingSpeedX = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.MarioStructAddress + Config.Mario.SlidingSpeedXOffset, 4), 0);
float slidingSpeedZ = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.MarioStructAddress + Config.Mario.SlidingSpeedZOffset, 4), 0);
float slidingSpeedX = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.StructAddress + Config.Mario.SlidingSpeedXOffset, 4), 0);
float slidingSpeedZ = BitConverter.ToSingle(_stream.ReadRam(Config.Mario.StructAddress + Config.Mario.SlidingSpeedZOffset, 4), 0);

_slidingSpeed.Text = ((float)Math.Sqrt(slidingSpeedX * slidingSpeedX + slidingSpeedZ * slidingSpeedZ)).ToString();

_fallHeight.Text = (BitConverter.ToSingle(_stream.ReadRam(Config.Mario.MarioStructAddress + Config.Mario.PeakHeightOffset, 4), 0) - floorY).ToString();
_fallHeight.Text = (BitConverter.ToSingle(_stream.ReadRam(Config.Mario.StructAddress + Config.Mario.PeakHeightOffset, 4), 0) - floorY).ToString();
}

private void RegisterControlEvents(Control control)
Expand All @@ -182,7 +182,7 @@ private void RegisterControlEvents(Control control)
private void OnDrag(object sender, EventArgs e)
{
// Start the drag and drop but setting the object slot index in Drag and Drop data
var dropAction = new DropAction(DropAction.ActionType.Mario, Config.Mario.MarioStructAddress);
var dropAction = new DropAction(DropAction.ActionType.Mario, Config.Mario.StructAddress);
(sender as Control).DoDragDrop(dropAction, DragDropEffects.All);
}

Expand Down
159 changes: 81 additions & 78 deletions Source/SM64 Diagnostic/Managers/ObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using SM64_Diagnostic.Controls;
using SM64_Diagnostic.Extensions;

namespace SM64_Diagnostic.ManagerClasses
{
public class ObjectManager
public class ObjectManager : DataManager
{
List<WatchVariableControl> _objectDataControls;
List<WatchVariableControl> _behaviorDataControls = new List<WatchVariableControl>();
ProcessStream _stream;
ObjectAssociations _objAssoc;
ObjectDataGui _objGui;

DataContainer _disToMario;
DataContainer _latDisToMario;
DataContainer _rngCalls;

object _watchVarLocker = new object();

uint _currentAddress;
Expand All @@ -39,7 +34,7 @@ public void SetBehaviorWatchVariables(List<WatchVariable> value, Color color)
// Remove old watchVars from list
foreach (var watchVar in _behaviorDataControls)
{
_objectDataControls.Remove(watchVar);
_dataControls.Remove(watchVar);
_objGui.ObjectFlowLayout.Controls.Remove(watchVar.Control);
}
_behaviorDataControls.Clear();
Expand All @@ -50,7 +45,7 @@ public void SetBehaviorWatchVariables(List<WatchVariable> value, Color color)
var newWatchVarControl = new WatchVariableControl(_stream, watchVar);
newWatchVarControl.Color = color;
_behaviorDataControls.Add(newWatchVarControl);
_objectDataControls.Add(newWatchVarControl);
_dataControls.Add(newWatchVarControl);
_objGui.ObjectFlowLayout.Controls.Add(newWatchVarControl.Control);
}
}
Expand All @@ -68,6 +63,10 @@ public uint? CurrentAddress
{
_currentAddress = value.HasValue ? value.Value : 0x0000;
_objGui.ObjAddressLabelValue.Text = "0x" + _currentAddress.ToString("X8");
foreach (WatchVariableControl watchVar in _dataControls)
{
watchVar.OtherOffset = _currentAddress;
}
}
}
}
Expand Down Expand Up @@ -164,56 +163,31 @@ public Image Image

#endregion

protected override void InitializeSpecialVariables()
{
_specialWatchVars = new List<DataContainer>()
{
new DataContainer("MarioDistanceToObject"),
new DataContainer("MarioLateralDistanceToObject"),
new DataContainer("MarioDistanceToObjectHome"),
new DataContainer("MarioLateralDistanceToObjectHome"),
new DataContainer("ObjectDistanceToHome"),
new DataContainer("LateralObjectDistanceToHome"),
new DataContainer("RngCallsPerFrame"),
};
}

public ObjectManager(ProcessStream stream, ObjectAssociations objAssoc, List<WatchVariable> objectData, ObjectDataGui objectGui)
: base(stream, objectData, objectGui.ObjectFlowLayout)
{
_stream = stream;
_objGui = objectGui;
_objAssoc = objAssoc;

// Register controls on the control (for drag-and-drop)
RegisterControlEvents(_objGui.ObjectBorderPanel);
foreach (Control control in _objGui.ObjectBorderPanel.Controls)
RegisterControlEvents(control);

_disToMario = new DataContainer("Dis. to Mario");
_latDisToMario = new DataContainer("Lat. Dis. to M");
_rngCalls = new DataContainer("RNG Calls/Frame");

_objectDataControls = new List<WatchVariableControl>();
foreach (WatchVariable watchVar in objectData)
{
if (!watchVar.Special)
{
WatchVariableControl watchControl = new WatchVariableControl(_stream, watchVar);
objectGui.ObjectFlowLayout.Controls.Add(watchControl.Control);
_objectDataControls.Add(watchControl);
continue;
}

switch (watchVar.SpecialType)
{
case "DistanceToMario":
_disToMario.Name = watchVar.Name;
objectGui.ObjectFlowLayout.Controls.Add(_disToMario.Control);
break;

case "LateralDistanceToMario":
_latDisToMario.Name = watchVar.Name;
objectGui.ObjectFlowLayout.Controls.Add(_latDisToMario.Control);
break;

case "RngCallsPerFrame":
_rngCalls.Name = watchVar.Name;
objectGui.ObjectFlowLayout.Controls.Add(_rngCalls.Control);
break;

default:
var failedContainer = new DataContainer(watchVar.Name);
failedContainer.Text = "Couldn't Find";
objectGui.ObjectFlowLayout.Controls.Add(failedContainer.Control);
break;
}
}

_objGui.ObjAddressLabelValue.Click += ObjAddressLabel_Click;
_objGui.ObjAddressLabel.Click += ObjAddressLabel_Click;

Expand Down Expand Up @@ -262,38 +236,69 @@ private void CloneButton_Click(object sender, EventArgs e)
MarioActions.CloneObject(_stream, CurrentAddress.Value);
}

public void Update()
private void ProcessSpecialVars()
{
lock (_watchVarLocker)
{
// Update watch variables
foreach (var watchVar in _objectDataControls)
{
watchVar.OtherOffset = CurrentAddress.HasValue ? CurrentAddress.Value : 0x0000;
watchVar.Update();
}
}

// Get Mario position
var marioAddress = Config.Mario.MarioStructAddress;
float mX, mY, mZ;
mX = BitConverter.ToSingle(_stream.ReadRam(marioAddress + Config.Mario.XOffset, 4), 0);
mY = BitConverter.ToSingle(_stream.ReadRam(marioAddress + Config.Mario.YOffset, 4), 0);
mZ = BitConverter.ToSingle(_stream.ReadRam(marioAddress + Config.Mario.ZOffset, 4), 0);
mX = _stream.GetSingle(Config.Mario.StructAddress + Config.Mario.XOffset);
mY = _stream.GetSingle(Config.Mario.StructAddress + Config.Mario.YOffset);
mZ = _stream.GetSingle(Config.Mario.StructAddress + Config.Mario.ZOffset);

// Get object position
float objX, objY, objZ;
objX = _stream.GetSingle(_currentAddress + Config.ObjectSlots.ObjectXOffset);
objY = _stream.GetSingle(_currentAddress + Config.ObjectSlots.ObjectYOffset);
objZ = _stream.GetSingle(_currentAddress + Config.ObjectSlots.ObjectZOffset);

// Get object position
float x, y, z;
x = BitConverter.ToSingle(_stream.ReadRam(_currentAddress + Config.ObjectSlots.ObjectXOffset, 4), 0);
y = BitConverter.ToSingle(_stream.ReadRam(_currentAddress + Config.ObjectSlots.ObjectYOffset, 4), 0);
z = BitConverter.ToSingle(_stream.ReadRam(_currentAddress + Config.ObjectSlots.ObjectZOffset, 4), 0);
float objHomeX, objHomeY, objHomeZ;
objHomeX = _stream.GetSingle(_currentAddress + Config.ObjectSlots.HomeXOffset);
objHomeY = _stream.GetSingle(_currentAddress + Config.ObjectSlots.HomeYOffset);
objHomeZ = _stream.GetSingle(_currentAddress + Config.ObjectSlots.HomeZOffset);

foreach (DataContainer specialVar in _specialWatchVars)
{
switch (specialVar.SpecialName)
{
case "MarioDistanceToObject":
specialVar.Text = MoreMath.DistanceTo(mX, mY, mZ, objX, objY, objZ).ToString();
break;

case "MarioLateralDistanceToObject":
specialVar.Text = MoreMath.DistanceTo(mX, mZ, objX, objZ).ToString();
break;

case "MarioDistanceToObjectHome":
specialVar.Text = MoreMath.DistanceTo(mX, mY, mZ, objHomeX, objHomeY, objHomeZ).ToString();
break;

case "MarioLateralDistanceToObjectHome":
specialVar.Text = MoreMath.DistanceTo(mX, mZ, objHomeX, objHomeZ).ToString();
break;

// Calculate distances to Mario
float latDisToMario = (float)Math.Sqrt(Math.Pow(x - mX, 2) + Math.Pow(z - mZ, 2));
float disToMario = (float)Math.Sqrt(Math.Pow(x - mX, 2) + Math.Pow(y - mY, 2) + Math.Pow(z - mZ, 2));
case "ObjectDistanceToHome":
specialVar.Text = MoreMath.DistanceTo(objX, objY, objZ, objHomeX, objHomeY, objHomeZ).ToString();
break;

case "LateralObjectDistanceToHome":
specialVar.Text = MoreMath.DistanceTo(objX, objZ, objHomeX, objHomeZ).ToString();
break;

case "RngCallsPerFrame":
specialVar.Text = GetNumRngCalls().ToString();
break;
}
}
}

public override void Update(bool updateView)
{
if (!updateView)
return;

// Determine which object is being held
uint holdingObj = BitConverter.ToUInt32(_stream.ReadRam(marioAddress + Config.Mario.HoldingObjectPointerOffset, 4),0);
uint holdingObj = _stream.GetUInt32(Config.Mario.StructAddress + Config.Mario.HoldingObjectPointerOffset);

// Change to unclone if we are already holding the object
if ((holdingObj == _currentAddress) != _unclone)
{
Expand All @@ -303,10 +308,8 @@ public void Update()
_objGui.CloneButton.Text = _unclone ? "UnClone" : "Clone";
}

// Update data container text
_latDisToMario.Text = latDisToMario.ToString();
_disToMario.Text = disToMario.ToString();
_rngCalls.Text = GetNumRngCalls().ToString();
base.Update(updateView);
ProcessSpecialVars();
}

private int GetNumRngCalls()
Expand Down
Loading

0 comments on commit a27c2e2

Please sign in to comment.