Example #1
0
        private void FindOrganisedConnectors()
        {
            myOrganisedConnectors = new ChunkConnector[4][][];

            for (int i = 0; i < 4; ++i)
            {
                int length = ((i % 2) == 0 ? Height : Width) - 2;

                myOrganisedConnectors[i] = new ChunkConnector[length][];

                List <ChunkConnector>[] lists = new List <ChunkConnector> [length];
                for (int j = 0; j < length; ++j)
                {
                    lists[j] = new List <ChunkConnector>();
                }

                foreach (ChunkConnector con in myConnectors[i])
                {
                    lists[con.Size - 1].Add(con);
                }

                for (int j = 0; j < length; ++j)
                {
                    myOrganisedConnectors[i][j] = lists[j].ToArray();
                }
            }
        }
Example #2
0
 public GenEndPoint(Chunk chunk, ChunkConnector connector, int depth)
 {
     X     = chunk.X + connector.X;
     Y     = chunk.Y + connector.Y;
     Size  = connector.Size;
     Face  = (ConnectorFace )((connector.Horizontal ? 0 : 1) + (connector.BottomOrRight ? 2 : 0));
     Chunk = chunk;
     Skin  = connector.Skin;
     Depth = depth;
 }
Example #3
0
        protected void LoadFromStream(BinaryReader stream)
        {
            myWidth  = stream.ReadInt16();
            myHeight = stream.ReadInt16();

            myTiles = new TileTemplate[Width, Height];

            for (int x = 0; x < Width; ++x)
            {
                for (int y = 0; y < Height; ++y)
                {
                    myTiles[x, y] = new TileTemplate();
                    myTiles[x, y].Load(stream);
                }
            }

            int types = stream.ReadInt16();

            myMapTypes = new DungeonClass[types];

            for (int i = 0; i < types; ++i)
            {
                myMapTypes[i] = DungeonClass.Get(stream.ReadString());
            }

            int entCount = stream.ReadInt16();

            myEntities = new Entity[entCount];

            for (int i = 0; i < entCount; ++i)
            {
                myEntities[i]             = Entity.Load(stream, false);
                myEntities[i].Probability = stream.ReadDouble();
            }

            myConnectors = new ChunkConnector[4][];

            for (int i = 0; i < 4; ++i)
            {
                int connectors = stream.ReadByte();

                myConnectors[i] = new ChunkConnector[connectors];

                for (int j = 0; j < connectors; ++j)
                {
                    myConnectors[i][j] = new ChunkConnector(stream, this);
                }
            }

            FindOrganisedConnectors();
        }
Example #4
0
        private GenChunkAddedStatus GenAddChunk(Random rand, Stack <GenEndPoint> endPoints, List <GenEndPoint> looseEnds, List <GenChunkRect> rects, ChunkTemplate[] templates, ref int area, int maxDepth = 0, bool first = false)
        {
            ChunkTemplate template;

            Chunk newChunk;
            List <GenEndPoint> endsToAdd;

            if (first)
            {
                template = templates[rand.Next(templates.Length)];
                newChunk = new Chunk(0, 0, template, this);
                Chunks.Add(newChunk);
                endsToAdd = new List <GenEndPoint>();
                for (int i = 0; i < 4; ++i)
                {
                    foreach (ChunkConnector connector in template.GetConnectors((ConnectorFace)i))
                    {
                        endsToAdd.Add(new GenEndPoint(newChunk, connector, 0));
                    }
                }

                while (endsToAdd.Count > 0)
                {
                    int index = rand.Next(endsToAdd.Count);
                    endPoints.Push(endsToAdd[index]);
                    endsToAdd.RemoveAt(index);
                }

                rects.Add(new GenChunkRect(newChunk));
                area += newChunk.Area;

                return(GenChunkAddedStatus.Added);
            }

            List <ChunkTemplate> validTemplates = new List <ChunkTemplate>();

            GenEndPoint endPoint = endPoints.Pop();

            ConnectorFace oppositeFace = (ConnectorFace)(((int)endPoint.Face + 2) % 4);

            foreach (ChunkTemplate temp in templates)
            {
                if (temp.GetConnectors(oppositeFace, endPoint.Size, endPoint.Skin).Length != 0)
                {
                    validTemplates.Add(temp);
                }
            }

            if (validTemplates.Count == 0)
            {
                endPoints.Push(endPoint);
                return(GenChunkAddedStatus.ImpossibleToAdd);
            }

            template = validTemplates[rand.Next(validTemplates.Count)];

            ChunkConnector[] cons = template.GetConnectors(oppositeFace, endPoint.Size, endPoint.Skin);

            if (cons.Length == 0)
            {
                endPoints.Push(endPoint);
                return(GenChunkAddedStatus.NotAdded);
            }

            ChunkConnector con = cons[rand.Next(cons.Length)];

            int x = endPoint.X - con.X;
            int y = endPoint.Y - con.Y;

            switch (endPoint.Face)
            {
            case ConnectorFace.Left:
                --x; break;

            case ConnectorFace.Top:
                --y; break;

            case ConnectorFace.Right:
                ++x; break;

            case ConnectorFace.Bottom:
                ++y; break;
            }

            GenChunkRect newRect = new GenChunkRect(x, y, template);

            foreach (GenChunkRect rect in rects)
            {
                if (rect.Intersects(newRect))
                {
                    endPoints.Push(endPoint);
                    return(GenChunkAddedStatus.NotAdded);
                }
            }

            newChunk = new Chunk(x, y, template, this);
            Chunks.Add(newChunk);
            endsToAdd = new List <GenEndPoint>();
            for (int i = 0; i < 4; ++i)
            {
                foreach (ChunkConnector connector in template.GetConnectors((ConnectorFace)i))
                {
                    if (connector.X != con.X || connector.Y != con.Y)
                    {
                        GenEndPoint end = new GenEndPoint(newChunk, connector, endPoint.Depth + 1);
                        if (maxDepth != 0 && endPoint.Depth >= maxDepth)
                        {
                            looseEnds.Add(end);
                        }
                        else
                        {
                            endsToAdd.Add(end);
                        }
                    }
                }
            }

            while (endsToAdd.Count > 0)
            {
                int index = rand.Next(endsToAdd.Count);
                endPoints.Push(endsToAdd[index]);
                endsToAdd.RemoveAt(index);
            }

            rects.Add(newRect);
            area += newChunk.Area;

            return(GenChunkAddedStatus.Added);
        }