Example #1
0
 public Target(Target copy)
 {
     this.Name = copy.Name;
     this.X_coordinate = copy.X_coordinate;
     this.Y_coordinate = copy.Y_coordinate;
     this.Z_coordinate = copy.Z_coordinate;
     this.Friend = copy.Friend;
 }
Example #2
0
 public override List<Target> ProcessFile()
 {
     XmlReader fr = XmlReader.Create(this.FilePath);
     List<Target> _output = new List<Target>();
     Target _current_target;
     while (fr.Read())  // read the xml file until the end.
     {
         string node_name = fr.Name.ToLower();
         switch (fr.NodeType)
         {
             case XmlNodeType.Element:
                 if (node_name != "targets" && node_name != "target") // if element is not named Targets or Target, file format is invalid.
                 {
                     throw new XmlException("Invalid Format.");
                 }
                 // if element is named Target, read it's attributes and store them as a Target.
                 else if (node_name == "target")
                 {
                     _current_target = new Target();
                     while (fr.MoveToNextAttribute())
                     {
                         // each attribute should be a target object attribute name and corresponding value.
                         string attribute_name = fr.Name.ToLower();
                         string attribute_value = fr.Value.ToLower();
                         if (attribute_name != "isfriend" && attribute_name != "name")
                         {
                             SetTargetPositionValue(_current_target, attribute_name, attribute_value);
                         }
                         else
                         {
                             if (attribute_name == "isfriend")
                             {
                                 SetTargetFriend(_current_target, attribute_value);
                             }
                             else if (attribute_name == "name")
                             {
                                 _current_target.Name = attribute_value;
                             }
                         }
                     }
                     _current_target.CalculateAngles();
                     _output.Add(_current_target);
                 }
                 break;
             case XmlNodeType.Comment:  // ignore comments
                 break;
             case XmlNodeType.XmlDeclaration: // ignore declarations
                 break;
             case XmlNodeType.Whitespace: // ignore whitespace
                 break;
             case XmlNodeType.EndElement:  // ignore end elements
                 break;
             default: // if defaulted, throw format is invalid.
                 throw new XmlException("Invalid Format.");
         }
     }
     return _output;
 }
Example #3
0
 public Target(Target copy)
 {
     this.Name = copy.Name;
     this.X_coordinate = copy.X_coordinate;
     this.Y_coordinate = copy.Y_coordinate;
     this.Z_coordinate = copy.Z_coordinate;
     this.Friend = copy.Friend;
     this.Destroyed = copy.Destroyed;
     this.Theta = copy.Theta;
     this.Phi = copy.Phi;
 }
Example #4
0
        /// <summary>
        /// Process ini file which processor was instantianted with.
        /// </summary>
        /// <returns> A List of Target objects</returns>
        /// <exception cref="InvalidIniFormat"></exception>
        public override List<Target> ProcessFile()
        {
            string[] fileLines = File.ReadAllLines(this.FilePath);
            List<Target> _output = new List<Target>();
            Target _current_target = null;
            foreach (string line in fileLines)
            {
                string trimedLine = line.Trim();
                if (trimedLine.StartsWith(";")) // ignore comments, continue on to next line.
                {
                    continue;
                }
                else if (string.IsNullOrEmpty(trimedLine)) // also ignore blank lines.
                {
                    continue;
                }
                else
                {
                    if (lineMatches(trimedLine)) // if line is a group declaration add new target to the list.
                    {
                        _current_target = new Target();
                        _output.Add(_current_target);
                    }
                    else // line is a key=value pair.
                    {
                        //
                        if (_current_target == null)
                        {
                            throw new InvalidIniFormat(_invalid_ini_format_message);
                        }
                        string[] keyvalue = trimedLine.Split('=');
                        string key = keyvalue[0].Trim().ToLower(); // grab key, trim whitespace, and make all lower.
                        string value = keyvalue[1].Trim().ToLower(); // same as above but for value.
                        if (key == "friend") // if the left side of the keyvalue pair is "isFriend" set the friend value of the target.
                        {
                            TargetSetFriend(_current_target, value);
                        }
                        else if (key == "name") // if the left side of the keyvalue pair is "name" set the name of the target.
                        {
                          _current_target.Name = value;
                        }
                        else
                        {

                            TargetSetPositionValue(_current_target, key, value);
                        }
                    }
                }
            }
            foreach (Target target in _output) { target.CalculateAngles(); }
            return _output;
        }
Example #5
0
 /// <summary>
 /// validate target destruction.
 /// </summary>
 /// <param name="currTarget">a target whose status is to be determined.</param>
 /// <returns></returns>
 public bool validate(Target currTarget)
 {
     /* for now we just return true if target is shot at, due to lack of anyway to actually validate*/
     return true;
 }
Example #6
0
 /// <summary>
 /// Method to add target a target one at a time to the target list.  
 /// </summary>
 /// <param name="new_name"></param>
 /// <param name="new_x"></param>
 /// <param name="new_y"></param>
 /// <param name="new_z"></param>
 /// <param name="friend"></param>
 public void AddTarget(double new_x, double new_y, double new_z, bool friend, string new_name = "")
 {
     Target tempTarget = new Target(new_name, new_x, new_y, new_z, friend);
     _targets.Add(tempTarget);
     if (TargetAdded != null)
     {
         TargetAdded();
     }
 }
Example #7
0
 /// <summary>
 /// Method to add target a target one at a time to the target list.  
 /// </summary>
 /// <param name="new_name"></param>
 /// <param name="new_x"></param>
 /// <param name="new_y"></param>
 /// <param name="new_z"></param>
 /// <param name="friend"></param>
 public void AddTarget(int new_x, int new_y, int new_z, bool friend, string new_name = "")
 {
     Target tempTarget = new Target(new_name, new_x, new_y, new_z, friend);
     _targets.Add(tempTarget);
     // notify observers of change, if necessary
     if (ChangedTargets != null)
     {
         ChangedTargets();
     }
 }
Example #8
0
 /// <summary>
 /// Set x,y,z position value, or throw an error if attribute_name isn't xpos, ypos, or zpos.
 /// </summary>
 /// <param name="_current_target">a Target obj</param>
 /// <param name="attribute_name">a string containing the attribute name</param>
 /// <param name="value">a string containing the attribue value</param>
 private void SetTargetPositionValue(Target _current_target, string attribute_name, string value)
 {
     switch (attribute_name)
     {
         case "xpos":
             _current_target.X_coordinate = Convert.ToDouble(value);
             break;
         case "ypos":
             _current_target.Y_coordinate = Convert.ToDouble(value);
             break;
         case "zpos":
             _current_target.Z_coordinate = Convert.ToDouble(value);
             break;
         default:
             throw new XmlException("Invalid Format");
     }
 }
Example #9
0
 /// <summary>
 /// Set the friend property of the target
 /// </summary>
 /// <param name="_current_target">A target object</param>
 /// <param name="attribute_value">a string containing "true" or "false"</param>
 private void SetTargetFriend(Target _current_target, string attribute_value)
 {
     if (attribute_value == "true")
     {
         _current_target.Friend = true;
     }
     else if (attribute_value == "false")
     {
         _current_target.Friend = false;
     }
     else
     {
         throw new XmlException("Invalid Format");
     }
 }
Example #10
0
 /// <summary>
 /// validate target destruction.
 /// </summary>
 /// <param name="currTarget">a target whose status is to be determined.</param>
 /// <returns></returns>
 public void validate(Target currTarget)
 {
     /* for now we just return true if target is shot at, due to lack of anyway to actually validate*/
     currTarget.Destroyed = true;
 }
Example #11
0
 /// <summary>
 /// Method to add target a target one at a time to the target list.  
 /// </summary>
 /// <param name="new_name"></param>
 /// <param name="new_x"></param>
 /// <param name="new_y"></param>
 /// <param name="new_z"></param>
 /// <param name="friend"></param>
 public void AddTarget(int new_x, int new_y, int new_z, bool friend, string new_name = "")
 {
     Target tempTarget = new Target(new_name, new_x, new_y, new_z, friend);
     _targets.Add(tempTarget);
 }
Example #12
0
 /// <summary>
 /// Set x,y,z position value of target
 /// </summary>
 /// <param name="_current_target">A target object</param>
 /// <param name="key">string containing the key</param>
 /// <param name="value">string containing the value</param>
 private void TargetSetPositionValue(Target _current_target, string key, string value)
 {
     /* try/catch exceptions from Convert operation, solely for the purpose of changing them to InvalidIniFormat exceptions */
     try
     {
         /* turn the left side of the key value pair into ASCII and check to make sure it is either x, y, or z, then add value to target if possible. */
         switch (key)
         {
             case "x":
                 _current_target.X_coordinate = Convert.ToDecimal(value);
                 break;
             case "y":
                 _current_target.Y_coordinate = Convert.ToDecimal(value);
                 break;
             case "z":
                 _current_target.Z_coordinate = Convert.ToDecimal(value);
                 break;
             default: // if it reaches this point, it is an invalid file
                 throw new InvalidIniFormat(_invalid_ini_format_message);
         }
     }
     catch (FormatException ex) // change exception from convert to invalidiniformat, but keep previous exception around for debugging.
     {
         throw new InvalidIniFormat(_invalid_ini_format_message, ex);
     }
     catch (OverflowException ex) // change exception from convert to invalidinitformat, but keep previous exception around for debugging.
     {
         throw new InvalidIniFormat(_invalid_ini_format_message, ex);
     }
 }
Example #13
0
 /// <summary>
 /// Set the friend property
 /// </summary>
 /// <param name="_current_target">A target object</param>
 /// <param name="value">string containing "true" or "false"</param>
 private void TargetSetFriend(Target _current_target, string value)
 {
     if (value == "yes")
     {
         _current_target.Friend = true;
     }
     else if (value == "no")
     {
         _current_target.Friend = false;
     }
     else
     {
         throw new InvalidIniFormat(_invalid_ini_format_message); // if the value is invalid throw exception.
     }
 }
Example #14
0
 public TargetInfo AttackingTarget(Target);
Example #15
0
 private int FastestPath(Target t1, Target t2)
 {
     if(t1.Theta == t2.Theta)
     {
         return 0;
     }
     /*if t1 and t2 thetas are both negatives..reverse the sort order, this achieves the effect
      * of the path being from largest to smallest negative(aka -1 is shot at before -2)
      * while in the other direction from smallest to largest.
      *  This results in firing at each target in order of the closest one first as the turret swings
      *  left, then it moves to all targets right of the 0,0 point firing at them in order*/
     else if (t1.Theta < 0 && t2.Theta < 0)
     {
         if(t1.Theta > t2.Theta)
         {
             return -1;
         }
     }
     else if (t1.Theta < t2.Theta)
     {
         return -1;
     }
     return 1;
 }