示例#1
0
        /// <summary>
        /// Finds tile index that is closest to specified orientation.
        /// </summary>
        /// <param name="orientation">Bit mask of tile orientation.</param>
        /// <returns>
        /// Zero-based index of tile in tileset.
        /// </returns>
        public int FindClosestIndexFromOrientation(int orientation)
        {
            //int[] map = (_style == AutotileStyle.tIDE) ? s_OrientationMap4 : s_OrientationMap8;

            int index = Array.IndexOf(s_OrientationMap8, orientation);

            if (index != -1)
            {
                return(index);
            }

            int strongestConnections = 2;
            int weakConnections      = 0;

            //int strongestOrientation = 0x00; // 00000000
            index = /*(_style == AutotileStyle.tIDE) ? 0 :*/ 15;

            for (int i = 0; i < s_OrientationMap8.Length; ++i)
            {
                int childOrientation = s_OrientationMap8[i];

                // If there are at least 3 strong connections...
                int s = OrientationUtility.CountStrongConnections(orientation, childOrientation);
                if (s > strongestConnections)
                {
                    // Strong connections overule any previous weak connection matches!
                    strongestConnections = s;
                    weakConnections      = OrientationUtility.CountWeakConnections(orientation, childOrientation);
                    //strongestOrientation = childOrientation;
                    index = i;
                }
                else if (s == strongestConnections)
                {
                    // Choose the connection that has the most weak connections!
                    int w = OrientationUtility.CountWeakConnections(orientation, childOrientation);
                    if (w > weakConnections)
                    {
                        //strongestOrientation = childOrientation;
                        index           = i;
                        weakConnections = w;
                    }
                }
            }

            return(index);
        }
        /// <summary>
        /// Finds mask of orientation that best matches the specified orientation mask.
        /// </summary>
        /// <param name="mask">Bit mask of tile orientation.</param>
        /// <returns>
        /// Bit mask of the closest available orientation.
        /// </returns>
        public int FindClosestOrientationMask(int mask)
        {
            // Is desired orientation available?
            if (this.FindOrientation(mask) != null)
            {
                return(mask);
            }

            // Find nearest match.
            int strongestConnections = 2;
            int weakConnections      = 0;
            int strongestOrientation = this.defaultOrientationMask;

            int childOrientation, s, w;

            if (FallbackMode == Tile.FallbackMode.NextBest)
            {
                for (int i = 0; i < this.orientations.Length; ++i)
                {
                    childOrientation = this.orientations[i].Mask;

                    // If there are at least 3 strong connections...
                    s = OrientationUtility.CountStrongConnections(mask, childOrientation);
                    if (s > strongestConnections)
                    {
                        // Strong connections overule any previous weak connection matches!
                        strongestConnections = s;
                        weakConnections      = OrientationUtility.CountWeakConnections(mask, childOrientation);
                        strongestOrientation = childOrientation;
                    }
                    // If this connection is just as strong as the previous then we can determine
                    // which one is better by looking at the weak connections.
                    else if (s == strongestConnections)
                    {
                        // Choose the connection that has the most weak connections!
                        w = OrientationUtility.CountWeakConnections(mask, childOrientation);
                        if (w > weakConnections)
                        {
                            strongestOrientation = childOrientation;
                            weakConnections      = w;
                        }
                    }
                }
            }
            else if (FallbackMode == Tile.FallbackMode.UseDefault)
            {
                for (int i = 0; i < this.orientations.Length; ++i)
                {
                    childOrientation = this.orientations[i].Mask;

                    // When using default mode there must be exactly 4 strong connections!
                    if (OrientationUtility.CountStrongConnections(mask, childOrientation) == 4)
                    {
                        w = OrientationUtility.CountWeakConnections(mask, childOrientation);

                        // Has a strong connection been found for the first time?
                        // Otherwise, choose the connection that has the most weak connections!
                        if (strongestConnections == 2 || w > weakConnections)
                        {
                            strongestConnections = 4;
                            weakConnections      = w;
                            strongestOrientation = childOrientation;
                        }
                    }
                }
            }
            else if (FallbackMode == Tile.FallbackMode.UseDefaultStrict)
            {
                for (int i = 0; i < this.orientations.Length; ++i)
                {
                    childOrientation = this.orientations[i].Mask;

                    // When using default mode there must be exactly 4 strong connections!
                    if (OrientationUtility.CountStrongConnections(mask, childOrientation) == 4 && OrientationUtility.CountWeakConnections(mask, childOrientation) == 4)
                    {
                        strongestOrientation = childOrientation;
                    }
                }
            }

            return(strongestOrientation);
        }