Exemple #1
0
        /// <summary>
        /// Merge two regions into one. Needs good testing
        /// </summary>
        /// <param name="otherRegion">The region to merge with</param>
        /// <returns>True if merged successfully, false if otherwise</returns>
        public bool MergeWithRegion(Region otherRegion)
        {
            RegionId thisId  = id;
            RegionId otherId = otherRegion.id;

            // Duplicate current neighbourhood.
            List <RegionId> thisConnected = new List <RegionId>();

            for (int i = 0; i < connections.Count; ++i)
            {
                thisConnected.Add(connections[i]);
            }
            List <RegionId> otherConnected = otherRegion.connections;

            // Find insertion point on this region
            int insertInThis = -1;

            for (int i = 0; i < thisConnected.Count; ++i)
            {
                if (thisConnected[i] == otherId)
                {
                    insertInThis = i;
                    break;
                }
            }

            if (insertInThis == -1)
            {
                return(false);
            }

            // Find insertion point on the other region
            int insertInOther = -1;

            for (int i = 0; i < otherConnected.Count; ++i)
            {
                if (otherConnected[i] == thisId)
                {
                    insertInOther = i;
                    break;
                }
            }

            if (insertInOther == -1)
            {
                return(false);
            }

            // Merge neighbours.
            connections = new List <RegionId>();
            for (int i = 0, ni = thisConnected.Count; i < ni - 1; ++i)
            {
                connections.Add(thisConnected[(insertInThis + 1 + i) % ni]);
            }

            for (int i = 0, ni = otherConnected.Count; i < ni - 1; ++i)
            {
                connections.Add(otherConnected[(insertInOther + 1 + i) % ni]);
            }

            RemoveAdjacentNeighbours();

            for (int j = 0; j < otherRegion.floors.Count; ++j)
            {
                AddUniqueFloorRegion(otherRegion.floors[j]);
            }
            spanCount            += otherRegion.spanCount;
            otherRegion.spanCount = 0;
            otherRegion.connections.Clear();

            return(true);
        }