Beispiel #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);
    }
Beispiel #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);
    }
Beispiel #3
0
    private void BakeMask(CellMap map, MazeMap maze, SegmentMask masks)
    {
        for (int i1 = 0; i1 < maze.Width(); i1++)
        {
            for (int j1 = 0; j1 < maze.Height(); j1++)
            {
                int segID = maze.GetSegment(new MapPos(i1, j1));

                for (int i2 = 0; i2 < masks.SegmentWidth(); i2++)
                {
                    for (int j2 = 0; j2 < masks.SegmentHeight(); j2++)
                    {
                        MapPos pos = new MapPos(i1 * masks.SegmentWidth() + i2, j1 * masks.SegmentHeight() + j2);
                        map.SetSegment(pos, masks.GetMask(segID).GetSegment(new MapPos(i2, j2)));
                    }
                }
            }
        }
    }