Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace test_map_generation.voronoi
- {
- public struct BiomeInfo
- {
- public char type;
- public int x;
- public int y;
- }
- public struct BiomeType
- {
- public char symbol;
- }
- public class voronoi_generation
- {
- public int map_size_width;
- public int map_size_height;
- public int starterBiomeCount;
- public BiomeType[] biomeTypes;
- public BiomeInfo[] biomeCollection;
- public char[,] field;
- public voronoi_generation(int width, int height, int numStarterBiomes)
- {
- this.map_size_width = width;
- this.map_size_height = height;
- this.starterBiomeCount = numStarterBiomes;
- this.field = new char[this.map_size_width, this.map_size_height];
- this.initBoimeStarters();
- }
- public void generateMap()
- {
- for (int i = 0; i < this.map_size_height; i++)
- {
- for (int j = 0; j < this.map_size_width; j++)
- {
- char nearest = '.'; // value here doesn't matter
- int dist = int.MaxValue;
- // walk over each biomeInfo
- for (int z = 0; z < this.biomeCollection.Length; z++)
- {
- // calculate the difference in x and y direction
- int xdiff = this.biomeCollection[z].x - i;
- int ydiff = this.biomeCollection[z].y - j;
- // calculate euclidean distance, sqrt is not needed
- // because we only compare and do not need the real value
- int cdist = xdiff * xdiff + ydiff * ydiff;
- // is the current distance smaller than the old distance?
- // if yes, take this biome
- if (cdist < dist)
- {
- nearest = this.biomeCollection[z].type;
- dist = cdist;
- }
- // set the field to the nearest biome
- this.field[j,i] = nearest;
- }
- }
- }
- }
- public void printMap()
- {
- for (int i = 0; i < this.map_size_height; i++)
- {
- String row = " ";
- for (int j = 0; j < this.map_size_width; j++)
- {
- row += this.field[j, i].ToString();
- }
- Console.WriteLine(row);
- }
- }
- private void initBoimeStarters()
- {
- this.initBiomeTypes();
- this.biomeCollection = new BiomeInfo[this.starterBiomeCount];
- for (int i = 0; i < this.starterBiomeCount; i++)
- {
- Random rngGen = new Random();
- char biomeTypeSelected = this.biomeTypes[rngGen.Next(this.biomeTypes.Length)].symbol;
- int positionX = rngGen.Next(this.map_size_width);
- int positionY = rngGen.Next(this.map_size_height);
- this.biomeCollection[i].type = biomeTypeSelected;
- this.biomeCollection[i].x = positionX;
- this.biomeCollection[i].y = positionY;
- }
- }
- public void initBiomeTypes()
- {
- this.biomeTypes = new BiomeType[8];
- this.biomeTypes[0].symbol = '#';
- this.biomeTypes[1].symbol = 'O';
- this.biomeTypes[2].symbol = '?';
- this.biomeTypes[3].symbol = 'x';
- this.biomeTypes[4].symbol = '@';
- this.biomeTypes[5].symbol = '%';
- this.biomeTypes[6].symbol = '-';
- this.biomeTypes[7].symbol = '+';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement