Пример #1
0
        public Bitmap GenImage(int wTiles, int hTiles)
        {
            int w = wTiles, h = hTiles;
            Bitmap img = new Bitmap(w, h);
            Random rng = new Random();

            Digraph<TileGenerationRecord> depGraph = MakeDependencyGraph(w, h);

            while (true)
            {
                IEnumerable<Digraph<TileGenerationRecord>.Vertex> tilesToGenerate =
                    depGraph.Vertices
                        .Where(v => (v.Label.IsGenerated == false))
                        .OrderBy(v => v.Predecessors.Count(vv => !vv.Label.IsGenerated));

                if (tilesToGenerate.Count() == 0)
                {
                    break;
                }

                Digraph<TileGenerationRecord>.Vertex nextTileToGenerate = tilesToGenerate.First();

                Console.WriteLine("{0}, {1}", nextTileToGenerate.Label.Point.X, nextTileToGenerate.Label.Point.Y);

                // actually generate the tile.
                SuccessorTileModel mergedModel = new SuccessorTileModel();

                // grab a list of all of the model positions represented by all the predecessors to this tile
                List<byte[]> positions =
                    GetPredecessorModelPositions(img, 1, nextTileToGenerate, new byte[] { }, this.maxN, 1, true, true);

                // look 'em all up
                foreach (byte[] pos in positions)
                {
                    if (pos.Length == 0)
                    {
                        mergedModel.AddAll(this.nullaryModel);
                    }
                    else
                    {
                        int n = pos.Length / 3;
                        SuccessorTileModel[] models = this.naryModels[n - 1].RangeSearch(pos, 2);

                        foreach (SuccessorTileModel model in models)
                        {
                            mergedModel.AddAll(model);
                        }
                    }
                }

                // choose randomly from that model
                Color c = mergedModel.ChooseRandomly(rng);

                if (c != null)
                {
                    img.SetPixel(nextTileToGenerate.Label.Point.X, nextTileToGenerate.Label.Point.Y, c);
                }

                nextTileToGenerate.Label.IsGenerated = true;
            }

            return img;
        }