public CoordHeading FindEntry(int Playfield, int DestinationNumber)
 {
     CoordHeading ret = new CoordHeading();
     ret.Coordinates.x = -1;
     foreach (WallCollision.Line l in WallCollision.Destinations[Playfield].Playfield.Lines)
     {
         if (l.ID != DestinationNumber)
         {
             continue;
         }
         ret.Coordinates.x = (l.LineStartPoint.X + l.LineEndPoint.X) / 2;
         ret.Coordinates.y = (l.LineStartPoint.Y + l.LineEndPoint.Y) / 2;
         ret.Coordinates.z = (l.LineStartPoint.Z + l.LineEndPoint.Z) / 2;
         // TODO: Calculate the right Quaternion for the heading...
         // - Algorithman
         Quaternion q =
             new Quaternion(
                 new Vector3(
                     (l.LineEndPoint.X - l.LineStartPoint.X), 1, -(l.LineEndPoint.Z - l.LineStartPoint.Z)));
         ret.Heading.x = q.x;
         ret.Heading.y = q.y;
         ret.Heading.z = q.z;
         ret.Heading.w = q.w;
     }
     return ret;
 }
        public static CoordHeading GetCoord(LineSegment lineSegment, float x, float z, AOCoord coordinates)
        {
            CoordHeading coordHeading = new CoordHeading();
            foreach (Line line in Destinations[lineSegment.ZoneToPlayfield].Playfield.Lines)
            {
                if (line.ID == lineSegment.ZoneToIndex)
                {
                    int incX = 0;
                    int incZ = 0;

                    Vector3 temp = new Vector3(
                        line.LineEndPoint.X - line.LineStartPoint.X, 0, line.LineEndPoint.Z - line.LineStartPoint.Z);

                    double factor = 1.0 / Math.Sqrt(Math.Pow(temp.x, 2) + Math.Pow(temp.z, 2));
                    temp.x = temp.x * factor;
                    temp.z = temp.z * factor;

                    if (line.LineStartPoint.X >= line.LineEndPoint.X)
                    {
                        coordHeading.Coordinates.x = line.LineEndPoint.X;
                        if (Math.Abs(lineSegment.VectorA.X - lineSegment.VectorB.X) >= 1)
                        {
                            if (lineSegment.VectorA.X > lineSegment.VectorB.X)
                            {
                                coordHeading.Coordinates.x += Math.Abs(line.LineEndPoint.X - line.LineStartPoint.X)
                                                              *
                                                              (Math.Abs(x - lineSegment.VectorB.X)
                                                               / Math.Abs(lineSegment.VectorA.X - lineSegment.VectorB.X));
                                incZ = 1;
                            }
                            else
                            {
                                coordHeading.Coordinates.x += Math.Abs(line.LineEndPoint.X - line.LineStartPoint.X)
                                                              *
                                                              (Math.Abs(x - lineSegment.VectorA.X)
                                                               / Math.Abs(lineSegment.VectorA.X - lineSegment.VectorB.X));
                                incZ = -1;
                            }
                        }
                    }
                    else
                    {
                        coordHeading.Coordinates.x = line.LineStartPoint.X;
                        if (Math.Abs(lineSegment.VectorA.X - lineSegment.VectorB.X) >= 1)
                        {
                            if (lineSegment.VectorA.X > lineSegment.VectorB.X)
                            {
                                coordHeading.Coordinates.x += Math.Abs(line.LineEndPoint.X - line.LineStartPoint.X)
                                                              *
                                                              (Math.Abs(x - lineSegment.VectorB.X)
                                                               / Math.Abs(lineSegment.VectorA.X - lineSegment.VectorB.X));
                                incZ = -1;
                            }
                            else
                            {
                                coordHeading.Coordinates.x += Math.Abs(line.LineEndPoint.X - line.LineStartPoint.X)
                                                              *
                                                              (Math.Abs(x - lineSegment.VectorA.X)
                                                               / Math.Abs(lineSegment.VectorA.X - lineSegment.VectorB.X));
                                incZ = 1;
                            }
                        }
                    }
                    if (line.LineStartPoint.Z >= line.LineEndPoint.Z)
                    {
                        coordHeading.Coordinates.z = line.LineEndPoint.Z;
                        if (Math.Abs(lineSegment.VectorA.Z - lineSegment.VectorB.Z) >= 1)
                        {
                            if (lineSegment.VectorA.Z > lineSegment.VectorB.Z)
                            {
                                coordHeading.Coordinates.z += Math.Abs(line.LineStartPoint.Z - line.LineEndPoint.Z)
                                                              *
                                                              (Math.Abs(z - lineSegment.VectorB.Z)
                                                               / Math.Abs(lineSegment.VectorA.Z - lineSegment.VectorB.Z));
                                incX = -1;
                            }
                            else
                            {
                                coordHeading.Coordinates.z += Math.Abs(line.LineStartPoint.Z - line.LineEndPoint.Z)
                                                              *
                                                              (Math.Abs(z - lineSegment.VectorA.Z)
                                                               / Math.Abs(lineSegment.VectorA.Z - lineSegment.VectorB.Z));
                                incX = 1;
                            }
                        }
                    }
                    else
                    {
                        coordHeading.Coordinates.z = line.LineStartPoint.Z;
                        if (Math.Abs(lineSegment.VectorA.Z - lineSegment.VectorB.Z) >= 1)
                        {
                            if (lineSegment.VectorA.Z > lineSegment.VectorB.Z)
                            {
                                coordHeading.Coordinates.z += Math.Abs(line.LineStartPoint.Z - line.LineEndPoint.Z)
                                                              *
                                                              (Math.Abs(z - lineSegment.VectorB.Z)
                                                               / Math.Abs(lineSegment.VectorA.Z - lineSegment.VectorB.Z));
                                incX = 1;
                            }
                            else
                            {
                                coordHeading.Coordinates.z += Math.Abs(line.LineStartPoint.Z - line.LineEndPoint.Z)
                                                              *
                                                              (Math.Abs(z - lineSegment.VectorA.Z)
                                                               / Math.Abs(lineSegment.VectorA.Z - lineSegment.VectorB.Z));
                                incX = -1;
                            }
                        }
                    }
                    if ((coordHeading.Coordinates.y < line.LineStartPoint.Y) || (coordinates.y < line.LineEndPoint.Y))
                    {
                        if (line.LineStartPoint.Y >= line.LineEndPoint.Y)
                        {
                            coordHeading.Coordinates.y = line.LineStartPoint.Y;
                        }
                        else
                        {
                            coordHeading.Coordinates.y = line.LineEndPoint.Y;
                        }
                    }
                    temp.x = temp.x * incZ * 4;
                    temp.z = temp.z * incX * 4;

                    coordHeading.Coordinates.x += Convert.ToSingle(temp.z);
                    coordHeading.Coordinates.z += Convert.ToSingle(temp.x);

                    temp.y = temp.x;
                    temp.x = -temp.z;
                    temp.z = temp.y;
                    temp.y = 0;
                    temp = temp.Normalize();
                    coordHeading.Heading = coordHeading.Heading.GenerateRotationFromDirectionVector(temp);
                    break;
                }
            }
            return coordHeading;
        }