示例#1
0
    public List <EnumDirection> GetEmptySides(MazeMap map, MapPos pos, EnumDirection?lastDir)
    {
        List <EnumDirection> list = new List <EnumDirection>();
        int curMask = map.GetSegment(pos);

        Predicate <EnumDirection> dirPre = delegate(EnumDirection d)
        {
            MapPos off = pos.Offset(d);

            if ((curMask & d.BitMask()) != 0)
            {
                return(false);
            }
            else if (map.IsValid(off) && (map.GetSegment(off) <= 0 || rand.NextDouble() < lpChance))
            {
                return(true);
            }

            return(false);
        };

        list.AddAll(MethodExtensions.AllDirections(), dirPre);

        if (lastDir.HasValue && rand.NextDouble() < strBias && list.Contains(lastDir.Value))
        {
            for (int n = list.Count - 1; n >= 0; n--)
            {
                list[n] = lastDir.Value;
            }
        }

        return(list);
    }
示例#2
0
    public MazeMap Generate(int sizeX, int sizeY)
    {
        MazeMap map = new MazeMap(sizeX, sizeY);

        List <MapPos> pending = new List <MapPos>();

        pending.Add(new MapPos(map.Width() / 2, map.Height() / 2));

        EnumDirection?lastDir = null;

        while (pending.Count > 0)
        {
            MapPos curPos             = pending[0];
            int    curSeg             = map.GetSegment(curPos);
            List <EnumDirection> dirs = GetEmptySides(map, curPos, lastDir);

            if (lastDir.HasValue)
            {
                curSeg |= lastDir.Value.Opposite().BitMask();
                map.SetSegment(curPos, curSeg);
            }

            if (dirs.Count <= 0)
            {
                lastDir = null;
                pending.RemoveAt(0);
                pending.Shuffle(rand);
            }
            else
            {
                EnumDirection nxtDir = dirs[rand.Next(dirs.Count)];
                curSeg |= nxtDir.BitMask();
                map.SetSegment(curPos, curSeg);
                lastDir = nxtDir;

                if (dirs.Count == 1)
                {
                    pending.RemoveAt(0);
                }

                pending.Insert(0, curPos.Offset(nxtDir));
            }
        }

        return(map);
    }
示例#3
0
    private int GetNeighbours(CellMap map, MapPos pos)
    {
        int total = 0;

        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                MapPos p2 = pos.Offset(i, j);

                if (!map.IsValid(p2))
                {
                    continue;
                }
                else if (!map.GetSegment(p2).SimpleState())
                {
                    total++;
                }
            }
        }

        return(total);
    }