Ejemplo n.º 1
0
        public static void Line(NMap map, NLocate src, NLocate dst, byte digType)
        {
            if (src.Equals(dst))
            {
                return;
            }
            if (map.WithIn(src))
            {
                map.SetBlock(src.X, src.Y, digType);
            }

            List <NLocate> mos = new List <NLocate>();

            mos.AddRange(src.Direction4());
            mos.Sort((x, y) => x.DistanceTo(dst).CompareTo(y.DistanceTo(dst)));
            Line(map, mos[0], dst, digType);
        }
Ejemplo n.º 2
0
        public double DistanceTo(NLocateSet set, out NLocate srcLoc, out NLocate dstLoc)
        {
            double min = double.MaxValue;

            srcLoc = new NLocate(0, 0);
            dstLoc = new NLocate(0, 0);
            foreach (var l in set.Set)
            {
                NLocate nearestLoc;
                var     dis = DistanceTo(l, out nearestLoc);
                if (dis < min)
                {
                    min    = dis;
                    srcLoc = nearestLoc;
                    dstLoc = l;
                }
            }
            return(min);
        }
Ejemplo n.º 3
0
        public static NMap CaveWallMap(int width, int height, float rate, int holeLeft)//dig map
        {
            NMap map = new NMap(width, height);

            map.Noise(rate);
            CellularAutomata2D rule = new CellularAutomata2D("s45678b5678");

            map = rule.Run(map, 3);
            var blobs = NBlob.Find(map, 255);

            blobs.FillByLeftBlob(map, holeLeft, 0);
            blobs.NoiseConnect(map, 255);

            map = map.InverseVal();
            bool    HorV     = RandomNum.Roll(0.5f);
            NLocate Entrance = new NLocate();
            NLocate Exit     = new NLocate();

            if (HorV)
            {
                Entrance = RandomSelect <NLocate> .Select(new List <NLocate>(map.LeftLocates(0))).Right();

                Exit = RandomSelect <NLocate> .Select(new List <NLocate>(map.RightLocates(0))).Left();
            }
            else
            {
                Entrance = RandomSelect <NLocate> .Select(new List <NLocate>(map.TopLocates(0))).Down();

                Exit = RandomSelect <NLocate> .Select(new List <NLocate>(map.BottomLocates(0))).Up();
            }
            map.SetBlock(Entrance.Square(1), 0);
            map.SetBlock(Exit.Square(1), 0);
            map.SetBlock(Entrance, (byte)DungeonBuilding.Port);
            map.SetBlock(Exit, (byte)DungeonBuilding.Port);

            NLocationRecogition.FindTreasure(map, 3);
            NLocationRecogition.FindDeadEnd(map, 3);

            NLocationRecogition.FindPassage(map, 4);

            return(map);
        }
Ejemplo n.º 4
0
        public void FindLocationPair(NMap map, byte val, out NLocate loc1, out NLocate loc2)
        {
            double  minVal  = double.MaxValue;
            NLocate minLoc1 = new NLocate(0, 0);
            NLocate minLoc2 = new NLocate(0, 0);

            for (int i = 1; i < Blobs.Count; i++)
            {
                NLocate tloc1, tloc2;
                var     dis = Blobs[0].DistanceTo(Blobs[i], out tloc1, out tloc2);
                if (dis < minVal)
                {
                    minVal  = dis;
                    minLoc1 = tloc1;
                    minLoc2 = tloc2;
                }
            }
            loc1 = minLoc1;
            loc2 = minLoc2;
        }
Ejemplo n.º 5
0
        public static void Noise(NMap map, NLocate src, NLocate dst, byte movType, byte digType)
        {
            if (src.Equals(dst))
            {
                return;
            }
            if (map.WithIn(src))
            {
                map.SetBlock(src.X, src.Y, digType);
            }

            List <NLocate> mos = new List <NLocate>();

            mos.AddRange(src.Direction4());
            mos.Sort((x, y) => x.DistanceTo(dst).CompareTo(y.DistanceTo(dst)));

            Dictionary <NLocate, double> rateDict = new Dictionary <NLocate, double>();

            if (map.WithIn(mos[0]))
            {
                rateDict.Add(mos[0], 0.6);
            }
            if (map.WithIn(mos[1]))
            {
                rateDict.Add(mos[1], 0.3);
            }
            if (map.WithIn(mos[2]))
            {
                rateDict.Add(mos[2], 0.1);
            }
            if (map.WithIn(mos[3]))
            {
                rateDict.Add(mos[3], 0.1);
            }

            NLocate mov = RandomSelectByRate <NLocate> .Select(rateDict);

            Noise(map, mov, dst, movType, digType);
        }
Ejemplo n.º 6
0
 static public void SelfTest()
 {
     {
         Console.WriteLine("B* SelfTest:");
         var     map = new NMap(20, 20);
         NLocate loc = new NLocate(10, 5);
         map.SetBlock(loc.Square(4), 255);
         loc = new NLocate(10, 10);
         map.SetBlock(loc.Square(3), 255);
         map.SetBlock(loc.LineTo(new NLocate(10, 17)), 255);
         loc = new NLocate(10, 17);
         map.SetBlock(loc.LineTo(new NLocate(2, 17)), 255);
         Console.WriteLine(map.Print());
         var path = BStar(new NLocate(0, 12), new NLocate(18, 9), map);
         foreach (var p in path)
         {
             map.SetBlock(p, 1);
         }
         Console.WriteLine(map.Print());
     }
     {
         Console.WriteLine("A* SelfTest:");
         var     map = new NMap(20, 20);
         NLocate loc = new NLocate(10, 5);
         map.SetBlock(loc.Square(4), 255);
         loc = new NLocate(10, 10);
         map.SetBlock(loc.Square(3), 255);
         map.SetBlock(loc.LineTo(new NLocate(10, 17)), 255);
         loc = new NLocate(10, 17);
         map.SetBlock(loc.LineTo(new NLocate(2, 17)), 255);
         Console.WriteLine(map.Print());
         var path = AStar(new NLocate(0, 12), new NLocate(18, 9), map);
         foreach (var p in path)
         {
             map.SetBlock(p, 1);
         }
         Console.WriteLine(map.Print());
     }
 }
Ejemplo n.º 7
0
        public static void SelfTest()
        {
            var loc    = new NLocate(10, 10);
            var revLoc = loc.Reverse(new NLocate(11, 10));

            Console.WriteLine("Reverse:" + revLoc.x + "," + revLoc.y);

            var branch = loc.Branch(new NLocate(11, 10));

            foreach (var b in branch)
            {
                Console.WriteLine("Branch:" + b.x + "," + b.y);
            }

            NMap map  = new NMap(20, 20);
            var  rect = new NLocate(9, 9).Square(5);

            map.SetBlock(rect, 255);
            Console.WriteLine(map.Print());

            map = new NMap(20, 20);
            var diam = new NLocate(9, 9).Diamond(5);

            map.SetBlock(diam, 255);
            Console.WriteLine(map.Print());

            map = new NMap(20, 20);
            var circle = new NLocate(9, 9).Circle(5);

            map.SetBlock(circle, 255);
            Console.WriteLine(map.Print());

            map = new NMap(20, 20);
            var line = new NLocate(9, 9).LineTo(new NLocate(19, 0));

            map.SetBlock(line, 255);
            Console.WriteLine(map.Print());
        }
Ejemplo n.º 8
0
        public static void SelfTest()
        {
            NLocate src = new NLocate(20, 20);
            NLocate dst = new NLocate(39, 39);

            NMap map = new NMap(40, 40, 255);

            Noise(map, src, dst, 0, 0);
            Noise(map, dst, new NLocate(0, 39), 0, 0);
            Console.WriteLine(map.Print());

            map = new NMap(40, 40, 255);
            Line(map, src, dst, 0);
            Console.WriteLine(map.Print());

            map = new NMap(40, 40, 255);
            AxisLine(map, src, dst, 0);
            Console.WriteLine(map.Print());

            map = new NMap(40, 40, 255);
            DrunkardWalk(map, src, 0);
            Console.WriteLine(map.Print());
        }
Ejemplo n.º 9
0
 public NLocate Reverse(NLocate loc)
 {
     return(new NLocate(x - (loc.x - x), y - (loc.y - y)));
 }
Ejemplo n.º 10
0
 public NLocate(NLocate src)
 {
     x = src.X;
     y = src.Y;
 }
Ejemplo n.º 11
0
        static public List <NLocate> AStar(NLocate src, NLocate dst, NMap map)
        {
            GPath.Clear();
            NPathNode  rootNode  = new NPathNode(src.X, src.Y);
            List <int> closeList = new List <int>();
            Dictionary <int, NPathNode> openList = new Dictionary <int, NPathNode>();

            openList.Add(src.ToIndex(map.Width), rootNode);
            //检查curPos可移动格(openList中尚未存在的,closeList中没有的)
            //并加入routeData OpenList(将其父类设置为curPos);
            //如果openList包含目标则从目标格开始返回结果
            //int count = 3;
            //float maxCost = start.distanceToPoint(goal)*4;
            while (openList.Count > 0)
            {
                //Console.WriteLine("CurrentLocate:" + rootNode.Locate.X + "*" + rootNode.Locate.Y);
                foreach (var l in rootNode.Locate.Direction4())
                {
                    if (l.Equals(dst))
                    {
                        return(new List <NLocate>(rootNode.GetPath()));
                    }

                    if (map.Alive(l) || (!map.WithIn(l)))
                    {
                        continue;
                    }

                    int index = l.ToIndex(map.Width);

                    if (!closeList.Contains(index))
                    {
                        if (!openList.ContainsKey(index))
                        {
                            openList.Add(index, rootNode.AddChild(l.X, l.Y));
                        }
                    }
                }
                int curIndex = rootNode.Locate.ToIndex(map.Width);
                openList.Remove(curIndex);
                closeList.Add(curIndex);

                //qDebug()<<__FUNCTION__<<__LINE__;
                //检查openList最小损耗格,将curPos加入closeList;将最小消耗格设curPos
                int minTotelCost = int.MaxValue;
                foreach (var l in openList.Values)
                {
                    if (l.TotelCost < minTotelCost)
                    {
                        minTotelCost = l.TotelCost;
                        rootNode     = l;
                    }
                }
            }

            if ((GPath != null) && GPath.Count > 0)
            {
                Console.WriteLine("find route!!!");
                return(GPath);
            }
            return(new List <NLocate>());
        }
Ejemplo n.º 12
0
 public NPathNode(int x, int y, NPathNode parent = null)
 {
     loc         = new NLocate(x, y);
     this.parent = parent;
 }
Ejemplo n.º 13
0
 public bool Alive(NLocate loc)
 {
     return(Alive(loc.X, loc.Y));
 }
Ejemplo n.º 14
0
 public void SetBlock(NLocate loc, byte val)
 {
     SetBlock(loc.X, loc.Y, val);
 }
Ejemplo n.º 15
0
 public byte GetBlock(NLocate loc)
 {
     return(GetBlock(loc.X, loc.Y));
 }