コード例 #1
0
        /// <summary>
        /// Check if the current solution is correct by checking if every sheep is within the correct area
        /// </summary>
        /// <param name="vd">The vertical decomposition</param>
        /// <returns>Number of wrong sheep</returns>
        public int CheckSolution(VerticalDecomposition vd)
        {
            int wrong = 0;

            foreach (GameObject sheep in this.m_sheep)
            {
                Vector2 sheep_pos = new Vector2(sheep.transform.position.x, sheep.transform.position.y);
                // Check if the owner of the area that the sheep is located in is equal to the sheeps owner
                Trapezoid trap = vd.Search(sheep_pos);
                Face      area = trap.bottom.face;
                // Debug.Log("Face corresponding to the area of the sheep position: " + area + "\nArea owner: " + area.owner + "\n" + trap.show());
                if (area.owner != sheep.GetComponent <OwnerScript>().GetOwner())
                {
                    wrong += 1;
                }
            }

            Debug.LogAssertion("The current solution is " + (wrong == 0 ? "correct!" : "wrong!") + "\n"
                               + (this.m_sheep.Count - wrong) + " out of " + this.m_sheep.Count + " correct");

            // Update shepherd count text
            UpdateText(wrong);

            continueButton.SetActive(wrong == 0);
            return(wrong);
        }
コード例 #2
0
        // Random endless level generation
        // Determines the amount of shepherds placed based on the current level number
        private ShepherdLevel CreateEndlessLevel(int level)
        {
            // create the output scriptable object
            var asset = ScriptableObject.CreateInstance <ShepherdLevel>();

            // place the shepherds and sheep randomly
            List <Vector2> shepherds = RandomPos(level + 4);
            List <Vector2> sheep     = RandomPos(2 * (level + 4));

            // Print locations
            string sls = "Shepherd locations: \n";

            foreach (Vector2 v in shepherds)
            {
                sls += "(" + v.x + ", " + v.y + "), ";
            }
            Debug.Log(sls);
            string shls = "Sheep locations: \n";

            foreach (Vector2 v in sheep)
            {
                shls += "(" + v.x + ", " + v.y + "), ";
            }
            Debug.Log(shls);

            // Construct the voronoi diagram corresponding to the shepherd locations
            StartVoronoi();
            foreach (Vector2 me in shepherds)
            {
                // Add vertex to the triangulation and update the voronoi
                Delaunay.AddVertex(m_delaunay, me);
                m_delaunay.SetOwner(me, Random.Range(0, 4));
                m_dcel = Voronoi.Create(m_delaunay);
            }

            // Create vertical decomposition
            VerticalDecomposition vd = VertDecomp(m_dcel);

            // Use the vertical decomposition to determine the ownership of each sheep
            // and add the sheep to the level
            foreach (Vector2 s in sheep)
            {
                Trapezoid trap = vd.Search(s);
                Face      area = trap.bottom.face;
                int       i    = area.owner;
                asset.addSheep(s, i);
            }

            // Normalize coordinates
            var rect = BoundingBoxComputer.FromPoints(asset.SheepList);

            asset.SheepList = Normalize(rect, 6f, asset.SheepList);

            // Set shepherd budget
            asset.setBudget(shepherds.Count);

            return(asset);
        }