Inheritance: Voronoi.Triangulation
Beispiel #1
0
        // Use this for initialization
        public void Start()
        {
            // create initial delaunay triangulation (three far-away points)
            m_delaunay = Delaunay.Create();

            // add auxiliary vertices as unowned
            foreach (var vertex in m_delaunay.Vertices)
            {
                m_ownership.Add(vertex, EOwnership.UNOWNED);
            }

            m_fishManager = new FishManager();

            // create polygon of rectangle window for intersection with voronoi
            float z          = Vector2.Distance(m_meshFilter.transform.position, Camera.main.transform.position);
            var   bottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, z));
            var   topRight   = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, z));

            m_meshRect = new Polygon2D(
                new List <Vector2>()
            {
                new Vector2(bottomLeft.x, bottomLeft.z),
                new Vector2(bottomLeft.x, topRight.z),
                new Vector2(topRight.x, topRight.z),
                new Vector2(topRight.x, bottomLeft.z)
            });

            VoronoiDrawer.CreateLineMaterial();
        }
Beispiel #2
0
        // Use this for initialization
        public void Start()
        {
            // create initial delaunay triangulation (three far-away points)
            m_delaunay = Delaunay.Create();

            // add auxiliary vertices as unowned
            foreach (var vertex in m_delaunay.Vertices)
            {
                m_ownership.Add(vertex, EOwnership.UNOWNED);
            }

            m_fishManager = new FishManager();

            // create polygon of rectangle window for intersection with voronoi
            float z          = Vector2.Distance(m_meshFilter.transform.position, Camera.main.transform.position);
            var   bottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, z));
            var   topRight   = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, z));

            m_meshRect = new Polygon2D(
                new List <Vector2>()
            {
                new Vector2(bottomLeft.x, bottomLeft.z),
                new Vector2(bottomLeft.x, topRight.z),
                new Vector2(topRight.x, topRight.z),
                new Vector2(topRight.x, bottomLeft.z)
            });

            if (useFirstPlayerAi)
            {
                GameObject goVoronoiAI1 = new GameObject("Ai player 1");
                m_voronoiAI1 = goVoronoiAI1.AddComponent <VoronoiAI>();
                m_voronoiAI1.SetPlayer1(true);

                m_voronoiAI1.SetCorners(bottomLeft, topRight);

                m_voronoiAI1.DrawBorders = true;
                m_voronoiAI1.DrawDelauneyTriangulation = true;
                m_voronoiAI1.DrawVoronoiDiagram        = true;

                StrategyHandler sh1 = new StrategyHandler()
                                      .Add(new OutsideCHStrategy(1f, new RandomStrategy(1)));
                //.Add(new GridStrategy(6, 4))
                //.Add(new RandomStrategy(25));
                //.Add(new LargestCellStrategy());
                m_voronoiAI1.SetStrategyHandler(sh1);
                // Select a score function used by the AI
                // new AreaScore();
                // new DistanceScore();
                // new StandardDeviationScore();
                // new CircumferenceScore();
                m_voronoiAI1.SetScoreFunction(new AreaScore());
            }
            if (useSecondPlayerAi)
            {
                GameObject goVoronoiAI2 = new GameObject("Ai player 2");

                m_voronoiAI2 = goVoronoiAI2.AddComponent <VoronoiAI>();
                m_voronoiAI2.SetPlayer1(false);
                m_voronoiAI2.SetCorners(bottomLeft, topRight);

                if (!useFirstPlayerAi)
                {
                    m_voronoiAI2.DrawBorders = true;
                    m_voronoiAI2.DrawDelauneyTriangulation = true;
                    m_voronoiAI2.DrawVoronoiDiagram        = true;
                }

                StrategyHandler sh2 = new StrategyHandler()
                                      //.Add(new OutsideCHStrategy(1f, new RandomStrategy(4)))
                                      //.Add(new GridStrategy(6, 4))
                                      .Add(new RandomStrategy(25));
                //.Add(new LargestCellStrategy());
                m_voronoiAI2.SetStrategyHandler(sh2);
                // Select a score function used by the AI
                // new AreaScore();
                // new DistanceScore();
                // new StandardDeviationScore();
                // new CircumferenceScore();
                m_voronoiAI2.SetScoreFunction(new AreaScore());
            }

            VoronoiDrawer.CreateLineMaterial();
        }
Beispiel #3
0
        /// <summary>
        /// Process a turn taken
        /// </summary>
        private void ProcessTurn()
        {
            if (m_halfTurnsTaken == 0)
            {
                // game has just been started
                m_GUIManager.OnStartClicked();
            }

            // load victory if screen clicked after every player has taken turn
            if (m_halfTurnsTaken >= 2 * m_turns)
            {
                if (m_playerArea[0] > m_playerArea[1])
                {
                    SceneManager.LoadScene(m_p1Victory);
                }
                else
                {
                    SceneManager.LoadScene(m_p2Victory);
                }
            }
            else
            {
                var me = new Vector2();
                if (player1Turn)
                {
                    if (useFirstPlayerAi)
                    {
                        // Let the AI generate a point
                        me = m_voronoiAI1.GetMove();
                    }
                    else
                    {
                        // obtain mouse position vector
                        var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                        pos.y = 0;
                        me    = new Vector2(pos.x, pos.z);
                    }
                }
                else
                {
                    if (useSecondPlayerAi)
                    {
                        // Let the AI generate a point
                        me = m_voronoiAI2.GetMove();
                    }
                    else
                    {
                        // obtain mouse position vector
                        var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                        pos.y = 0;
                        me    = new Vector2(pos.x, pos.z);
                    }
                }

                // check if vertex already in graph to avoid degenerate cases
                if (m_ownership.ToList().Exists(v => MathUtil.EqualsEps(v.Key, me)))
                {
                    return;
                }

                // Store the vertex in the data structure of the AI
                if (useFirstPlayerAi)
                {
                    m_voronoiAI1.AddMove(me, player1Turn);
                    // Log the area computed by the voronoi AI
                    float[] areas = m_voronoiAI1.gs.Voronoi.ComputeArea();
                    Debug.Log("Area: " + areas[0] + " - " + areas[1]);
                }
                if (useSecondPlayerAi)
                {
                    m_voronoiAI2.AddMove(me, player1Turn);
                    if (!useFirstPlayerAi)
                    {
                        // Log the area computed by the voronoi AI
                        float[] areas = m_voronoiAI2.gs.Voronoi.ComputeArea();
                        Debug.Log("Area: " + areas[0] + " - " + areas[1]);
                    }
                }


                // store owner of vertex
                m_ownership.Add(me, player1Turn ? EOwnership.PLAYER1 : EOwnership.PLAYER2);

                Delaunay.AddVertex(m_delaunay, me);

                // instantiate the relevant game object at click position
                var prefab        = player1Turn ? m_Player1Prefab : m_Player2Prefab;
                var onClickObject = Instantiate(prefab, new Vector3(me.x, 0, me.y), Quaternion.identity) as GameObject;

                if (onClickObject == null)
                {
                    throw new InvalidProgramException("Couldn't instantiate m_PlayerPrefab!");
                }

                // set parent to this game object for better nesting
                onClickObject.transform.parent = gameObject.transform;

                // add object to the fish manager
                m_fishManager.AddFish(onClickObject.transform, player1Turn, m_withLookAtOnPlacement);

                UpdateVoronoi();

                // update player turn
                player1Turn = !player1Turn;
                m_GUIManager.OnTurnStart(player1Turn);

                //Update turn counter
                m_halfTurnsTaken += 1;
                if (m_halfTurnsTaken >= 2 * m_turns)
                {
                    m_GUIManager.OnLastMove();
                }
            }
        }