Advertisement
BurningBunny

Rouge-like automata dungeon map generation

Jul 9th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. /*
  2.  * Automata procedual dungeon generation proof-of-concept
  3.  *
  4.  * http://csharpcodewhisperer.blogspot.com/
  5.  *
  6.  *
  7.  */
  8. using System;
  9. using System.Collections.Generic;
  10. using Microsoft.Win32;
  11.  
  12. namespace AutomataMapGenerator
  13. {
  14.     public class MapHandler
  15.     {
  16.         Random rand = new Random();
  17.        
  18.         public int[,] Map;
  19.        
  20.         public int MapWidth     { get; set; }
  21.         public int MapHeight        { get; set; }
  22.         public int PercentAreWalls  { get; set; }
  23.        
  24.         public MapHandler()
  25.         {
  26.             MapWidth = 40;
  27.             MapHeight = 21;
  28.             PercentAreWalls = 40;
  29.                
  30.             RandomFillMap();
  31.         }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.         public void MakeCaverns()
  38.         {
  39.             // By initilizing column in the outter loop, its only created ONCE
  40.             for(int column=0, row=0; row <= MapHeight-1; row++)
  41.             {
  42.                 for(column = 0; column <= MapWidth-1; column++)
  43.                 {
  44.                     Map[column,row] = PlaceWallLogic(column,row);
  45.                 }
  46.             }
  47.         }
  48.        
  49.         public int PlaceWallLogic(int x,int y)
  50.         {
  51.             int numWalls = GetAdjacentWalls(x,y,1,1);
  52.  
  53.            
  54.             if(Map[x,y]==1)
  55.             {
  56.                 if( numWalls >= 4 )
  57.                 {
  58.                     return 1;
  59.                 }
  60.                 if(numWalls<2)
  61.                 {
  62.                     return 0;
  63.                 }
  64.                
  65.             }
  66.             else
  67.             {
  68.                 if(numWalls>=5)
  69.                 {
  70.                     return 1;
  71.                 }
  72.             }
  73.             return 0;
  74.         }
  75.        
  76.         public int GetAdjacentWalls(int x,int y,int scopeX,int scopeY)
  77.         {
  78.             int startX = x - scopeX;
  79.             int startY = y - scopeY;
  80.             int endX = x + scopeX;
  81.             int endY = y + scopeY;
  82.            
  83.             int iX = startX;
  84.             int iY = startY;
  85.            
  86.             int wallCounter = 0;
  87.            
  88.             for(iY = startY; iY <= endY; iY++) {
  89.                 for(iX = startX; iX <= endX; iX++)
  90.                 {
  91.                     if(!(iX==x && iY==y))
  92.                     {
  93.                         if(IsWall(iX,iY))
  94.                         {
  95.                             wallCounter += 1;
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.             return wallCounter;
  101.         }
  102.        
  103.         bool IsWall(int x,int y)
  104.         {
  105.             // Consider out-of-bound a wall
  106.             if( IsOutOfBounds(x,y) )
  107.             {
  108.                 return true;
  109.             }
  110.            
  111.             if( Map[x,y]==1  )
  112.             {
  113.                 return true;
  114.             }
  115.            
  116.             if( Map[x,y]==0  )
  117.             {
  118.                 return false;
  119.             }
  120.             return false;
  121.         }
  122.        
  123.         bool IsOutOfBounds(int x, int y)
  124.         {
  125.             if( x<0 || y<0 )
  126.             {
  127.                 return true;
  128.             }
  129.             else if( x>MapWidth-1 || y>MapHeight-1 )
  130.             {
  131.                 return true;
  132.             }
  133.             return false;
  134.         }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.         public void PrintMap()
  143.         {
  144.             Console.Clear();
  145.             Console.Write(MapToString());
  146.         }
  147.        
  148.         string MapToString()
  149.         {
  150.             string returnString = string.Join(" ", // Seperator between each element
  151.                                               "Width:",
  152.                                               MapWidth.ToString(),
  153.                                               "\tHeight:",
  154.                                               MapHeight.ToString(),
  155.                                               "\t% Walls:",
  156.                                               PercentAreWalls.ToString(),
  157.                                               Environment.NewLine
  158.                                              );
  159.            
  160.             List<string> mapSymbols = new List<string>();
  161.             mapSymbols.Add(".");
  162.             mapSymbols.Add("#");
  163.             mapSymbols.Add("+");
  164.            
  165.             for(int column=0,row=0; row < MapHeight; row++ ) {
  166.                 for( column = 0; column < MapWidth; column++ )
  167.                 {
  168.                     returnString += mapSymbols[Map[column,row]];
  169.                 }
  170.                 returnString += Environment.NewLine;
  171.             }
  172.             return returnString;
  173.         }
  174.        
  175.         public void BlankMap()
  176.         {
  177.             for(int column=0,row=0; row < MapHeight; row++) {
  178.                 for(column = 0; column < MapWidth; column++) {
  179.                     Map[column,row] = 0;
  180.                 }
  181.             }
  182.         }
  183.        
  184.         public void RandomFillMap()
  185.         {
  186.             // New, empty map
  187.             Map = new int[MapWidth,MapHeight];
  188.            
  189.             int mapMiddle = 0; // Temp variable
  190.             for(int column=0,row=0; row < MapHeight; row++) {
  191.                 for(column = 0; column < MapWidth; column++)
  192.                 {
  193.                     // If coordinants lie on the the edge of the map (creates a border)
  194.                     if(column == 0)
  195.                     {
  196.                         Map[column,row] = 1;
  197.                     }
  198.                     else if (row == 0)
  199.                     {
  200.                         Map[column,row] = 1;
  201.                     }
  202.                     else if (column == MapWidth-1)
  203.                     {
  204.                         Map[column,row] = 1;
  205.                     }
  206.                     else if (row == MapHeight-1)
  207.                     {
  208.                         Map[column,row] = 1;
  209.                     }
  210.                     // Else, fill with a wall a random percent of the time
  211.                     else
  212.                     {
  213.                         mapMiddle = (MapHeight / 2);
  214.                        
  215.                         if(row == mapMiddle)
  216.                         {
  217.                             Map[column,row] = 0;
  218.                         }
  219.                         else
  220.                         {
  221.                             Map[column,row] = RandomPercent(PercentAreWalls);
  222.                         }
  223.                     }
  224.                 }
  225.             }
  226.         }
  227.  
  228.         int RandomPercent(int percent)
  229.         {
  230.             if(percent>=rand.Next(1,101))
  231.             {
  232.                 return 1;
  233.             }
  234.             return 0;
  235.         }
  236.        
  237.         public MapHandler(int mapWidth, int mapHeight, int[,] map, int percentWalls=40)
  238.         {
  239.             this.MapWidth = mapWidth;
  240.             this.MapHeight = mapHeight;
  241.             this.PercentAreWalls = percentWalls;
  242.             this.Map = new int[this.MapWidth,this.MapHeight];
  243.             this.Map = map;
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249.  
  250.  
  251. /*
  252.  * Automata procedual dungeon generation proof-of-concept
  253.  *
  254.  * http://csharpcodewhisperer.blogspot.com/
  255.  *
  256.  * Made using SharpDevelop
  257.  *
  258.  *
  259.  *
  260.  */
  261. namespace DungeonGenerator
  262. {
  263.     using AutomataMapGenerator;
  264.     class Program
  265.     {
  266.         public static void Main(string[] args)
  267.         {
  268.             char key        = new Char();
  269.             MapHandler Map = new MapHandler();
  270.            
  271.             string instructions =
  272.                 "[Q]uit [N]ew [+][-]Percent walls [R]andom [B]lank" + Environment.NewLine +
  273.                 "Press any other key to smooth/step";
  274.  
  275.             Map.MakeCaverns();
  276.             Map.PrintMap();
  277.             Console.WriteLine(instructions);
  278.            
  279.             key = Char.ToUpper(Console.ReadKey(true).KeyChar);
  280.             while(!key.Equals('Q'))
  281.             {
  282.                 if(key.Equals('+')) {
  283.                     Map.PercentAreWalls+=1;
  284.                     Map.RandomFillMap();
  285.                     Map.MakeCaverns();
  286.                     Map.PrintMap();
  287.                 } else if(key.Equals('-')) {
  288.                     Map.PercentAreWalls-=1;
  289.                     Map.RandomFillMap();
  290.                     Map.MakeCaverns();
  291.                     Map.PrintMap();
  292.                 } else if(key.Equals('R')) {
  293.                     Map.RandomFillMap();
  294.                     Map.PrintMap();
  295.                 } else if(key.Equals('N')) {
  296.                     Map.RandomFillMap();
  297.                     Map.MakeCaverns();
  298.                     Map.PrintMap();
  299.                 } else if(key.Equals('B')) {
  300.                     Map.BlankMap();
  301.                     Map.PrintMap();
  302.                 } else if(key.Equals('D')) {
  303.                     // I set a breakpoint here...
  304.                 } else {
  305.                     Map.MakeCaverns();
  306.                     Map.PrintMap();
  307.                 }
  308.                 Console.WriteLine(instructions);
  309.                 key = Char.ToUpper(Console.ReadKey(true).KeyChar);
  310.             }
  311.             Console.Clear();
  312.             Console.Write(" Thank you for playing!");
  313.             Console.ReadKey(true);
  314.         }
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement