예제 #1
0
        public void TestDropout()
        {
            GameState gameState = TestGameState(5, 5);
            DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood);

            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;

            gameState.Set(new Tile[, ]
            {
                { _, _, _, _, _ },
                { _, _, _, _, _ },
                { _, _, f, _, _ },
                { _, _, _, _, _ },
                { _, _, _, _, _ }
            });
            dut.Propagate();

            gameState.Set(new Tile[, ]
            {
                { _, _, _, _, _ },
                { _, _, _, _, _ },
                { _, _, _, _, _ },
                { _, _, _, _, _ },
                { _, _, _, _, _ }
            });
            dut.Propagate();



            Assert.AreEqual(DistanceField <GameState.Tile> .Max, dut.GetDistance(2, 2));

            Assert.AreEqual(DistanceField <GameState.Tile> .Max, dut.GetDistance(3, 2));
            Assert.AreEqual(DistanceField <GameState.Tile> .Max, dut.GetDistance(4, 2));
        }
예제 #2
0
        public void Exploration1()
        {
            GameState gameState = TestGameState(7, 10);
            DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.terrain == GameState.Terrain.Unknown);

            const Tile u = Tile.Unseen;
            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;
            const Tile w = Tile.Water;

            Tile[,] map = new Tile[, ]
            {
                { w, w, w, w, w, w, w },
                { w, _, _, _, _, _, _ },
                { w, _, _, _, _, _, _ },
                { w, _, _, _, _, _, _ },
                { w, _, _, _, _, _, _ },
                { w, _, _, _, _, _, _ },
                { w, _, _, _, _, _, _ },
                { w, _, _, _, u, _, _ },
                { w, _, _, _, _, _, _ },
                { w, _, _, _, _, _, _ },
            };

            gameState.Set(map);
            dut.Propagate();

            {
                Assert.AreEqual(0, dut.GetDistance(4, 7));
                Assert.AreEqual(1, dut.GetDistance(3, 7));
                Assert.AreEqual(2, dut.GetDistance(2, 7));
                Assert.AreEqual(3, dut.GetDistance(2, 6));
                Assert.AreEqual(4, dut.GetDistance(2, 5));
            }
        }
예제 #3
0
        public void TestWater()
        {
            GameState gameState = TestGameState(7, 5);
            DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood);

            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;
            const Tile w = Tile.Water;

            Tile[,] map = new Tile[, ]
            {
                { _, w, _, w, _, w, _ },
                { _, w, _, w, _, w, _ },
                { _, w, f, w, _, _, _ },
                { _, _, w, w, w, w, _ },
                { _, w, _, _, _, w, _ }
            };

            gameState.Set(map);
            dut.Propagate();

            Assert.AreEqual(0, dut.GetDistance(2, 2));
            Assert.AreEqual(1, dut.GetDistance(2, 1));
            Assert.AreEqual(2, dut.GetDistance(2, 0));
            Assert.AreEqual(12, dut.GetDistance(6, 4));
        }
예제 #4
0
        public void TestWrap()
        {
            DistanceField dut = new DistanceField(7, 5, Tile.Food);

            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;
            const Tile w = Tile.Water;

            Tile[,] map = new Tile[, ]
            {
                { f, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ }
            };

            dut.Propagate(map, 20);

            Assert.AreEqual(0, dut.GetDistance(0, 0));
            Assert.AreEqual(1, dut.GetDistance(0, 1));
            Assert.AreEqual(1, dut.GetDistance(1, 0));
            Assert.AreEqual(1, dut.GetDistance(0, 4));
            Assert.AreEqual(1, dut.GetDistance(6, 0));
        }
예제 #5
0
        public void TestBasic_NotSquare()
        {
            GameState gameState = TestGameState(7, 5);
            DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isFood);

            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;

            Tile[,] map = new Tile[, ]
            {
                { _, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ },
                { _, _, f, _, _, _, _ },
                { _, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ }
            };

            gameState.Set(map);

            dut.Propagate();

            Assert.AreEqual(0, dut.GetDistance(2, 2));

            Assert.AreEqual(1, dut.GetDistance(3, 2));
            Assert.AreEqual(2, dut.GetDistance(4, 2));

            Assert.AreEqual(1, dut.GetDistance(2, 3));
            Assert.AreEqual(2, dut.GetDistance(2, 4));

            Assert.AreEqual(1, dut.GetDistance(2, 1));
            Assert.AreEqual(2, dut.GetDistance(2, 0));

            Assert.AreEqual(1, dut.GetDistance(1, 2));
            Assert.AreEqual(2, dut.GetDistance(0, 2));
        }
예제 #6
0
        public void TestBasic_NotSquare()
        {
            DistanceField dut = new DistanceField(7, 5, Tile.Food);

            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;

            Tile[,] map = new Tile[, ]
            {
                { _, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ },
                { _, _, f, _, _, _, _ },
                { _, _, _, _, _, _, _ },
                { _, _, _, _, _, _, _ }
            };

            dut.Propagate(map, 10);

            Assert.AreEqual(0, dut.GetDistance(2, 2));

            Assert.AreEqual(1, dut.GetDistance(3, 2));
            Assert.AreEqual(2, dut.GetDistance(4, 2));

            Assert.AreEqual(1, dut.GetDistance(2, 3));
            Assert.AreEqual(2, dut.GetDistance(2, 4));

            Assert.AreEqual(1, dut.GetDistance(2, 1));
            Assert.AreEqual(2, dut.GetDistance(2, 0));

            Assert.AreEqual(1, dut.GetDistance(1, 2));
            Assert.AreEqual(2, dut.GetDistance(0, 2));
        }
예제 #7
0
        public void Exploration2()
        {
            GameState gameState = TestGameState(7, 10);
            DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.terrain == GameState.Terrain.Unknown);

            const Tile u = Tile.Unseen;
            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;
            const Tile w = Tile.Water;

            Tile[,] map = new Tile[, ]
            {
                { w, w, w, w, w, w, w },
                { w, _, _, _, _, _, u },
                { w, _, _, _, _, _, u },
                { w, _, _, _, _, _, u },
                { w, _, _, _, _, _, u },
                { w, _, _, _, _, _, u },
                { w, _, _, _, _, _, u },
                { w, w, w, w, w, _, u },
                { w, _, _, _, _, _, u },
                { w, u, u, u, u, u, u },
            };

            gameState.Set(map);
            dut.Propagate();

            {
                Assert.AreEqual(5, dut.GetDistance(1, 1));
                Assert.AreEqual(5, dut.GetDistance(1, 2));
                Assert.AreEqual(0, dut.GetDistance(6, 2));
                Assert.AreEqual(1, dut.GetDistance(5, 2));
                Assert.AreEqual(5, dut.GetDistance(1, 6));
            }

            {
                Vector2i result = dut.GetDescent(1, 6).First();
                Assert.AreEqual(1, result.x);
                Assert.AreEqual(0, result.y);
            }
        }
예제 #8
0
 void FixedUpdate()
 {
     for (int i = 0; i < orbiters.Length; i++)
     {
         Orbiter orbiter = orbiters[i];
         Vector3 normal;
         float   dist = DistanceField.GetDistance(orbiter.position.x, orbiter.position.y, orbiter.position.z, out normal);
         orbiter.velocity -= normal.normalized * attraction * Mathf.Clamp(dist, -1f, 1f);
         orbiter.velocity += Random.insideUnitSphere * jitter;
         orbiter.velocity *= .99f;
         orbiter.position += orbiter.velocity;
         Color targetColor;
         if (dist > 0f)
         {
             targetColor = Color.Lerp(surfaceColor, exteriorColor, dist / exteriorColorDist);
         }
         else
         {
             targetColor = Color.Lerp(surfaceColor, interiorColor, -dist / interiorColorDist);
         }
         orbiter.color = Color.Lerp(orbiter.color, targetColor, Time.deltaTime * colorStiffness);
         orbiters[i]   = orbiter;
     }
 }
예제 #9
0
        public void ProblemCase3_AntHills()
        {
            GameState gameState = TestGameState(7, 10);
            DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.isEnemyHill);

            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;
            const Tile w = Tile.Water;
            const Tile h = Tile.TheirHill;
            const Tile u = Tile.Unseen;

            gameState.Set(new Tile[, ]
            {
                { _, w, w, _, _, _, w },
                { _, w, w, h, _, _, w },
                { _, w, w, _, _, _, w },
                { w, w, w, _, _, _, w },
                { w, w, w, _, _, _, w },
                { _, _, _, _, _, _, w },
                { _, _, _, _, _, _, w },
                { _, _, _, _, _, _, w },
                { _, _, _, _, _, _, w },
                { w, w, w, w, w, w, w },
            });

            dut.Propagate();

            Assert.AreEqual(1, dut.GetDistance(3, 2));

            {
                Vector2i r = dut.GetDescent(2, 5).First();
                Assert.AreEqual(1, r.x);
                Assert.AreEqual(0, r.y);
            }

            {
                Vector2i r = dut.GetDescent(1, 5).First();
                Assert.AreEqual(1, r.x);
                Assert.AreEqual(0, r.y);
            }



            gameState.Set(new Tile[, ]
            {
                { _, w, w, _, _, _, w },
                { _, w, w, u, _, _, w },
                { _, w, w, _, _, _, w },
                { w, w, w, _, _, _, w },
                { w, w, w, _, _, _, w },
                { _, _, _, _, _, _, w },
                { _, _, _, _, _, _, w },
                { _, _, _, _, _, _, w },
                { _, _, _, _, _, _, w },
                { w, w, w, w, w, w, w },
            });

            dut.Propagate();

            Assert.AreEqual(1, dut.GetDistance(3, 2));

            {
                Vector2i r = dut.GetDescent(2, 5).First();
                Assert.AreEqual(1, r.x);
                Assert.AreEqual(0, r.y);
            }

            {
                Vector2i r = dut.GetDescent(1, 5).First();
                Assert.AreEqual(1, r.x);
                Assert.AreEqual(0, r.y);
            }
        }
예제 #10
0
        public void Exploration3()
        {
            GameState gameState = TestGameState(7, 10);
            DistanceField <GameState.Tile> dut = new DistanceField <GameState.Tile>(gameState, gameState.map, tile => tile.terrain == GameState.Terrain.Unknown);

            const Tile u = Tile.Unseen;
            const Tile f = Tile.Food;
            const Tile _ = Tile.Land;
            const Tile w = Tile.Water;



            gameState.Set(new Tile[, ]
            {
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { w, w, w, _, w, w, w },
                { w, w, w, _, w, w, w },
                { w, w, w, _, w, w, w },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u }
            });
            dut.Propagate();
            {
                Assert.AreEqual(0, dut.GetDistance(3, 2));
                Assert.AreEqual(1, dut.GetDistance(3, 3));
                Assert.AreEqual(2, dut.GetDistance(3, 4));
                Assert.AreEqual(1, dut.GetDistance(3, 5));
                Assert.AreEqual(0, dut.GetDistance(3, 6));
            }

            gameState.Set(new Tile[, ]
            {
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { w, w, w, _, w, w, w },
                { w, w, w, _, w, w, w },
                { w, w, w, _, w, w, w },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u }
            });
            dut.Propagate();
            {
                Assert.AreEqual(0, dut.GetDistance(3, 1));
                Assert.AreEqual(1, dut.GetDistance(3, 2));
                Assert.AreEqual(2, dut.GetDistance(3, 3));
                Assert.AreEqual(2, dut.GetDistance(3, 4));
                Assert.AreEqual(1, dut.GetDistance(3, 5));
                Assert.AreEqual(0, dut.GetDistance(3, 6));


                Vector2i r = dut.GetDescent(3, 2).First();
                Assert.AreEqual(0, r.x);
                Assert.AreEqual(-1, r.y);
            }


            gameState.Set(new Tile[, ]
            {
                { u, u, u, u, u, u, u },
                { w, w, w, _, w, w, w },
                { w, w, w, _, w, w, w },
                { w, w, w, _, w, w, w },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u },
                { u, u, u, u, u, u, u }
            });
            dut.Propagate();
            {
                Assert.AreEqual(0, dut.GetDistance(3, 0));
                Assert.AreEqual(1, dut.GetDistance(3, 1));
                Assert.AreEqual(2, dut.GetDistance(3, 2));
                Assert.AreEqual(3, dut.GetDistance(3, 3));
                Assert.AreEqual(2, dut.GetDistance(3, 4));
                Assert.AreEqual(1, dut.GetDistance(3, 5));
                Assert.AreEqual(0, dut.GetDistance(3, 6));

                Vector2i r = dut.GetDescent(3, 2).First();
                Assert.AreEqual(0, r.x);
                Assert.AreEqual(-1, r.y);
            }
        }