Great idea Apoc... let me share my revamp of the pathbody stuff (still doing pathheader - was outta town for a week).
Obviously, this does not reflect your above suggestion, but it HEAVILY leverages what your other post about pathing covers, as you will see. I have yet to test this, so not sure if it works yet. That said, I feel pretty good about understanding the concepts.
As an aside, given that I really do have two very distinct entries in my path (headers with no waypoints that are really enabling the setting of flags later, and then the body elements with waypoints), doesn't it make sense to actually set up two classes to manage it??
Code:
/*
* This struct is used to track all of the body entries for
* the path. The PathHeader class is used for the header
* entries.
*
* Command words that indicate the entry is a body row are:
* Goto
* PauseAt
* GatherAt
* IgnoreAll
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
using System.IO;
namespace ROXBotD1
{
public struct PathBody : IEquatable<PathBody>
{
public string COMMAND { get; set; }
public float X { get; set; }
public float Y { get; set; }
public string COMPARAM { get; set; }
public PathBody(string command, float x, float y, string comparam):this()
{
COMMAND = command;
X = x;
Y = y;
COMPARAM = comparam;
}
public PathBody(XElement xml)
: this(Convert.ToString(xml.Name),
Convert.ToSingle(xml.Attribute("X").Value),
Convert.ToSingle(xml.Attribute("Y").Value),
Convert.ToString(xml.Attribute("Param").Value))
{}
public double Distance(PathBody to)
{
return Distance(to.X, to.Y);
}
public double Distance(float toX, float toY)
{
float dX = X - toX;
float dY = Y - toY;
return Math.Sqrt(dX * dX + dY * dY);
}
public XElement GetXml()
{
return new XElement(COMMAND, new XAttribute("X", X), new XAttribute("Y", Y), new XAttribute("Param", COMPARAM));
}
public static List<PathBody> LoadObject(ListBox lbname)
{
var ret = new List<PathBody>();
XElement lbxml = XElement.Parse("");
for (int i = 0; i < lbname.Items.Count; i++)
{
lbxml.AddAfterSelf(XElement.Parse(lbname.Items[i].ToString()));
}
foreach (XElement pb in lbxml.Elements())
{
if (pb.Name == "Goto" || pb.Name == "PauseAt" || pb.Name == "GatherAt" || pb.Name == "IgnoreAll")
ret.Add(new PathBody(pb));
}
return ret;
}
public static List<PathBody> LoadFile(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException("Could not find the specified file!", filePath);
}
var ret = new List<PathBody>();
XElement file = XElement.Load(filePath);
foreach (XElement pb in file.Elements())
{
if (pb.Name == "Goto" || pb.Name == "PauseAt" || pb.Name == "GatherAt" || pb.Name == "IgnoreAll")
ret.Add(new PathBody(pb));
}
return ret;
}
#region IEquatable<PathBody> Members
public bool Equals(PathBody other)
{
return other.COMMAND == COMMAND && other.X == X && other.Y == Y && other.COMPARAM == COMPARAM;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (obj.GetType() != typeof(PathBody))
{
return false;
}
return Equals((PathBody)obj);
}
public override int GetHashCode()
{
unchecked
{
int result = COMMAND.ToString().GetHashCode();
result = (result * 397) ^ X.GetHashCode();
result = (result * 397) ^ Y.GetHashCode();
result = (result * 397) ^ COMPARAM.ToString().GetHashCode();
return result;
}
}
public static bool operator ==(PathBody left, PathBody right)
{
return left.Equals(right);
}
public static bool operator !=(PathBody left, PathBody right)
{
return !left.Equals(right);
}
#endregion
}
}