Difference between revisions of "C-Sharp Example of Dungeon-Building Algorithm"

From RogueBasin
Jump to navigation Jump to search
m (removed all the empty lines.)
m (Hexman moved page Java Example of Dungeon-Building Algorithm to C-Sharp Example of Dungeon-Building Algorithm: The code example given is in C#. Though syntactically similar, C# is a different programming language created by Microsoft for the .N...)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
(Made by [[Solarnus]])
(Made by [[Solarnus]])
<source lang="java">
import java.util.*; //and for getting today's date


public class dungen{
This code is C#, not Java
//max size of the map
private int xmax = 80; //80 columns
private int ymax = 25; //25 rows


//size of the map
private int xsize = 0;
private int ysize = 0;
//number of "objects" to generate on the map
private int objects = 0;
//define the %chance to generate either a room or a corridor on the map
//BTW, rooms are 1st priority so actually it's enough to just define the chance
//of generating a room
private int chanceRoom = 75;
private int chanceCorridor = 25;


//our map
<source lang="csharp">
private int[] dungeon_map = { };
//the old seed from the RNG is saved in this one
private long oldseed = 0;
//a list over tile types we're using
final private int tileUnused = 0;
final private int tileDirtWall = 1; //not in use
final private int tileDirtFloor = 2;
final private int tileStoneWall = 3;
final private int tileCorridor = 4;
final private int tileDoor = 5;
final private int tileUpStairs = 6;
final private int tileDownStairs = 7;
final private int tileChest = 8;
//misc. messages to print
private String msgXSize = "X size of dungeon: \t";
private String msgYSize = "Y size of dungeon: \t";
private String msgMaxObjects = "max # of objects: \t";
private String msgNumObjects = "# of objects made: \t";
private String msgHelp = "";
private String msgDetailedHelp = "";


    public class Dungeon
      {
   
        // misc. messages to print
        const string MsgXSize = "X size of dungeon: \t";
   
        const string MsgYSize = "Y size of dungeon: \t";
   
        const string MsgMaxObjects = "max # of objects: \t";
   
        const string MsgNumObjects = "# of objects made: \t";
   
        // max size of the map
        int xmax = 80; //columns
        int ymax = 25; //rows
   
        // size of the map
        int _xsize;
        int _ysize;
   
        // number of "objects" to generate on the map
        int _objects;
   
        // define the %chance to generate either a room or a corridor on the map
        // BTW, rooms are 1st priority so actually it's enough to just define the chance
        // of generating a room
        const int ChanceRoom = 75;
   
        // our map
        Tile[] _dungeonMap = { };
   
        readonly IRandomize _rnd;
   
        readonly Action<string> _logger;
   
   
        public Dungeon(IRandomize rnd, Action<string> logger)
        {
            _rnd = rnd;
            _logger = logger;
        }
       
        public int Corridors
        {
            get;
            private set;
        }
   
        public static bool IsWall(int x, int y, int xlen, int ylen, int xt, int yt, Direction d)
        {
            Func<int, int, int> a = GetFeatureLowerBound;
           
            Func<int, int, int> b = IsFeatureWallBound;
            switch (d)
            {
                case Direction.North:
                    return xt == a(x, xlen) || xt == b(x, xlen) || yt == y || yt == y - ylen + 1;
                case Direction.East:
                    return xt == x || xt == x + xlen - 1 || yt == a(y, ylen) || yt == b(y, ylen);
                case Direction.South:
                    return xt == a(x, xlen) || xt == b(x, xlen) || yt == y || yt == y + ylen - 1;
                case Direction.West:
                    return xt == x || xt == x - xlen + 1 || yt == a(y, ylen) || yt == b(y, ylen);
            }
           
            throw new InvalidOperationException();
        }
   
        public static int GetFeatureLowerBound(int c, int len)
        {
            return c - len / 2;
        }
   
        public static int IsFeatureWallBound(int c, int len)
        {
            return c + (len - 1) / 2;
        }
   
        public static int GetFeatureUpperBound(int c, int len)
        {
            return c + (len + 1) / 2;
        }
   
        public static IEnumerable<PointI> GetRoomPoints(int x, int y, int xlen, int ylen, Direction d)
        {
            // north and south share the same x strategy
            // east and west share the same y strategy
            Func<int, int, int> a = GetFeatureLowerBound;
            Func<int, int, int> b = GetFeatureUpperBound;
   
            switch (d)
            {
                case Direction.North:
                    for (var xt = a(x, xlen); xt < b(x, xlen); xt++) for (var yt = y; yt > y - ylen; yt--) yield return new PointI { X = xt, Y = yt };
                    break;
                case Direction.East:
                    for (var xt = x; xt < x + xlen; xt++) for (var yt = a(y, ylen); yt < b(y, ylen); yt++) yield return new PointI { X = xt, Y = yt };
                    break;
                case Direction.South:
                    for (var xt = a(x, xlen); xt < b(x, xlen); xt++) for (var yt = y; yt < y + ylen; yt++) yield return new PointI { X = xt, Y = yt };
                    break;
                case Direction.West:
                    for (var xt = x; xt > x - xlen; xt--) for (var yt = a(y, ylen); yt < b(y, ylen); yt++) yield return new PointI { X = xt, Y = yt };
                    break;
                default:
                    yield break;
            }
        }
   
        public Tile GetCellType(int x, int y)
        {
            try
            {
                return this._dungeonMap[x + this._xsize * y];
            }
            catch (IndexOutOfRangeException)
            {
                new { x, y }.Dump("exceptional");
                throw;
            }
        }
   
        public int GetRand(int min, int max)
        {
            return _rnd.Next(min, max);
        }
   
        public bool MakeCorridor(int x, int y, int length, Direction direction)
        {
            // define the dimensions of the corridor (er.. only the width and height..)
            int len = this.GetRand(2, length);
            const Tile Floor = Tile.Corridor;
   
            int xtemp;
            int ytemp = 0;
   
            switch (direction)
            {
                case Direction.North:
                    // north
                    // check if there's enough space for the corridor
                    // start with checking it's not out of the boundaries
                    if (x < 0 || x > this._xsize) return false;
                    xtemp = x;
   
                    // same thing here, to make sure it's not out of the boundaries
                    for (ytemp = y; ytemp > (y - len); ytemp--)
                    {
                        if (ytemp < 0 || ytemp > this._ysize) return false; // oh boho, it was!
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
   
                    // if we're still here, let's start building
                    Corridors++;
                    for (ytemp = y; ytemp > (y - len); ytemp--)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
   
                    break;
   
                case Direction.East:
                    // east
                    if (y < 0 || y > this._ysize) return false;
                    ytemp = y;
   
                    for (xtemp = x; xtemp < (x + len); xtemp++)
                    {
                        if (xtemp < 0 || xtemp > this._xsize) return false;
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
   
                    Corridors++;
                    for (xtemp = x; xtemp < (x + len); xtemp++)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
   
                    break;
   
                case Direction.South:
                    // south
                    if (x < 0 || x > this._xsize) return false;
                    xtemp = x;
                   
                    for (ytemp = y; ytemp < (y + len); ytemp++)
                    {
                        if (ytemp < 0 || ytemp > this._ysize) return false;
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
                   
                    Corridors++;
                    for (ytemp = y; ytemp < (y + len); ytemp++)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
                   
                    break;
                case Direction.West:
                    // west
                    if (ytemp < 0 || ytemp > this._ysize) return false;
                    ytemp = y;
                   
                    for (xtemp = x; xtemp > (x - len); xtemp--)
                    {
                        if (xtemp < 0 || xtemp > this._xsize) return false;
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
                   
                    Corridors++;
                    for (xtemp = x; xtemp > (x - len); xtemp--)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
                   
                    break;
            }


//setting a tile's type
            // woot, we're still here! let's tell the other guys we're done!!
private void setCell(int x, int y, int celltype){
            return true;
dungeon_map[x + xsize * y] = celltype;
        }
}
//returns the type of a tile
private int getCell(int x, int y){
return dungeon_map[x + xsize * y];
}
//The RNG. the seed is based on seconds from the "java epoch" ( I think..)
//perhaps it's the same date as the unix epoch
private int getRand(int min, int max){
//the seed is based on current date and the old, already used seed
Date now = new Date();
long seed = now.getTime() + oldseed;
oldseed = seed;
Random randomizer = new Random(seed);
int n = max - min + 1;
int i = randomizer.nextInt(n);
if (i < 0)
i = -i;
//System.out.println("seed: " + seed + "\tnum:  " + (min + i));
return min + i;
}
private boolean makeCorridor(int x, int y, int lenght, int direction){
//define the dimensions of the corridor (er.. only the width and height..)
int len = getRand(2, lenght);
int floor = tileCorridor;
int dir = 0;
if (direction > 0 && direction < 4) dir = direction;
int xtemp = 0;
int ytemp = 0;
switch(dir){
case 0:
//north
//check if there's enough space for the corridor
//start with checking it's not out of the boundaries
if (x < 0 || x > xsize) return false;
else xtemp = x;
//same thing here, to make sure it's not out of the boundaries
for (ytemp = y; ytemp > (y-len); ytemp--){
if (ytemp < 0 || ytemp > ysize) return false; //oh boho, it was!
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
//if we're still here, let's start building
for (ytemp = y; ytemp > (y-len); ytemp--){
setCell(xtemp, ytemp, floor);
}
break;
case 1:
//east
if (y < 0 || y > ysize) return false;
else ytemp = y;
for (xtemp = x; xtemp < (x+len); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
for (xtemp = x; xtemp < (x+len); xtemp++){
setCell(xtemp, ytemp, floor);
}
break;
case 2:
//south
if (x < 0 || x > xsize) return false;
else xtemp = x;
for (ytemp = y; ytemp < (y+len); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
for (ytemp = y; ytemp < (y+len); ytemp++){
setCell(xtemp, ytemp, floor);
}
break;
case 3:
//west
if (ytemp < 0 || ytemp > ysize) return false;
else ytemp = y;
for (xtemp = x; xtemp > (x-len); xtemp--){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
for (xtemp = x; xtemp > (x-len); xtemp--){
setCell(xtemp, ytemp, floor);
}
break;
}
//woot, we're still here! let's tell the other guys we're done!!
return true;
}
private boolean makeRoom(int x, int y, int xlength, int ylength, int direction){
//define the dimensions of the room, it should be at least 4x4 tiles (2x2 for walking on, the rest is walls)
int xlen = getRand(4, xlength);
int ylen = getRand(4, ylength);
//the tile type it's going to be filled with
int floor = tileDirtFloor; //jordgolv..
int wall = tileDirtWall; //jordv????gg
//choose the way it's pointing at
int dir = 0;
if (direction > 0 && direction < 4) dir = direction;


switch(dir){
        public IEnumerable<Tuple<PointI, Direction>> GetSurroundingPoints(PointI v)
case 0:
        {
//north
            var points = new[]
//Check if there's enough space left for it
                            {
for (int ytemp = y; ytemp > (y-ylen); ytemp--){
                                Tuple.Create(new PointI { X = v.X, Y = v.Y + 1 }, Direction.North),
if (ytemp < 0 || ytemp > ysize) return false;
                                Tuple.Create(new PointI { X = v.X - 1, Y = v.Y }, Direction.East),
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
                                Tuple.Create(new PointI { X = v.X , Y = v.Y-1 }, Direction.South),
if (xtemp < 0 || xtemp > xsize) return false;
                                Tuple.Create(new PointI { X = v.X +1, Y = v.Y  }, Direction.West),
if (getCell(xtemp, ytemp) != tileUnused) return false; //no space left...
                               
}
                            };
}
            return points.Where(p => InBounds(p.Item1));
        }
//we're still here, build
for (int ytemp = y; ytemp > (y-ylen); ytemp--){
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
//start with the walls
if (xtemp == (x-xlen/2)) setCell(xtemp, ytemp, wall);
else if (xtemp == (x+(xlen-1)/2)) setCell(xtemp, ytemp, wall);
else if (ytemp == y) setCell(xtemp, ytemp, wall);
else if (ytemp == (y-ylen+1)) setCell(xtemp, ytemp, wall);
//and then fill with the floor
else setCell(xtemp, ytemp, floor);
}
}
break;
case 1:
//east
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
if (ytemp < 0 || ytemp > ysize) return false;
for (int xtemp = x; xtemp < (x+xlen); xtemp++){
if (xtemp < 0 || xtemp > xsize) return false;
if (getCell(xtemp, ytemp) != tileUnused) return false;
}
}


for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
        public IEnumerable<Tuple<PointI, Direction, Tile>> GetSurroundings(PointI v)
for (int xtemp = x; xtemp < (x+xlen); xtemp++){
        {
            return
if (xtemp == x) setCell(xtemp, ytemp, wall);
                this.GetSurroundingPoints(v)
else if (xtemp == (x+xlen-1)) setCell(xtemp, ytemp, wall);
                    .Select(r => Tuple.Create(r.Item1, r.Item2, this.GetCellType(r.Item1.X, r.Item1.Y)));
else if (ytemp == (y-ylen/2)) setCell(xtemp, ytemp, wall);
        }
else if (ytemp == (y+(ylen-1)/2)) setCell(xtemp, ytemp, wall);
 
        public bool InBounds(int x, int y)
else setCell(xtemp, ytemp, floor);
        {
}
            return x > 0 && x < this.xmax && y > 0 && y < this.ymax;
}
        }
break;
 
case 2:
        public bool InBounds(PointI v)
//south
        {
for (int ytemp = y; ytemp < (y+ylen); ytemp++){
            return this.InBounds(v.X, v.Y);
if (ytemp < 0 || ytemp > ysize) return false;
        }
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
 
if (xtemp < 0 || xtemp > xsize) return false;
        public bool MakeRoom(int x, int y, int xlength, int ylength, Direction direction)
if (getCell(xtemp, ytemp) != tileUnused) return false;
        {
}
            // define the dimensions of the room, it should be at least 4x4 tiles (2x2 for walking on, the rest is walls)
}
            int xlen = this.GetRand(4, xlength);
            int ylen = this.GetRand(4, ylength);
for (int ytemp = y; ytemp < (y+ylen); ytemp++){
 
for (int xtemp = (x-xlen/2); xtemp < (x+(xlen+1)/2); xtemp++){
            // the tile type it's going to be filled with
            const Tile Floor = Tile.DirtFloor;
if (xtemp == (x-xlen/2)) setCell(xtemp, ytemp, wall);
 
else if (xtemp == (x+(xlen-1)/2)) setCell(xtemp, ytemp, wall);
            const Tile Wall = Tile.DirtWall;
else if (ytemp == y) setCell(xtemp, ytemp, wall);
            // choose the way it's pointing at
else if (ytemp == (y+ylen-1)) setCell(xtemp, ytemp, wall);
 
            var points = GetRoomPoints(x, y, xlen, ylen, direction).ToArray();
else setCell(xtemp, ytemp, floor);
 
}
            // Check if there's enough space left for it
}
            if (
break;
                points.Any(
case 3:
                    s =>
//west
                    s.Y < 0 || s.Y > this._ysize || s.X < 0 || s.X > this._xsize || this.GetCellType(s.X, s.Y) != Tile.Unused)) return false;
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
            _logger(
if (ytemp < 0 || ytemp > ysize) return false;
                      string.Format(
for (int xtemp = x; xtemp > (x-xlen); xtemp--){
                          "Making room:int x={0}, int y={1}, int xlength={2}, int ylength={3}, int direction={4}",
if (xtemp < 0 || xtemp > xsize) return false;
                          x,
if (getCell(xtemp, ytemp) != tileUnused) return false;  
                          y,
}
                          xlength,
}
                          ylength,
                          direction));
for (int ytemp = (y-ylen/2); ytemp < (y+(ylen+1)/2); ytemp++){
 
for (int xtemp = x; xtemp > (x-xlen); xtemp--){
            foreach (var p in points)
            {
if (xtemp == x) setCell(xtemp, ytemp, wall);
                this.SetCell(p.X, p.Y, IsWall(x, y, xlen, ylen, p.X, p.Y, direction) ? Wall : Floor);
else if (xtemp == (x-xlen+1)) setCell(xtemp, ytemp, wall);
            }
else if (ytemp == (y-ylen/2)) setCell(xtemp, ytemp, wall);
 
else if (ytemp == (y+(ylen-1)/2)) setCell(xtemp, ytemp, wall);
            // yay, all done
            return true;
else setCell(xtemp, ytemp, floor);
        }
}
 
}
        public Tile[] GetDungeon()
break;
        {
}
            return this._dungeonMap;
        }
//yay, all done
 
return true;
        public char GetCellTile(int x, int y)
}
        {
            switch (GetCellType(x, y))
            {
//used to print the map on the screen
                case Tile.Unused:
public void showDungeon(){
                    return '';
for (int y = 0; y < ysize; y++){
                case Tile.DirtWall:
for (int x = 0; x < xsize; x++){
                    return '|';
//System.out.print(getCell(x, y));
                case Tile.DirtFloor:
switch(getCell(x, y)){
                    return '_';
case tileUnused:
                case Tile.StoneWall:
System.out.print(" ");
                    return 'S';
break;
                case Tile.Corridor:
case tileDirtWall:
                    return '#';
System.out.print("+");
                case Tile.Door:
break;
                    return 'D';
case tileDirtFloor:
                case Tile.Upstairs:
System.out.print(".");
                    return '+';
break;
                case Tile.Downstairs:
case tileStoneWall:
                    return '-';
System.out.print("O");
                case Tile.Chest:
break;
                    return 'C';
case tileCorridor:
                default:
System.out.print("#");
                    throw new ArgumentOutOfRangeException("x,y");
break;
            }
case tileDoor:
        }
System.out.print("D");
 
break;
        //used to print the map on the screen
case tileUpStairs:
        public void ShowDungeon()
System.out.print("<");
        {
break;
            for (int y = 0; y < this._ysize; y++)
case tileDownStairs:
            {
System.out.print(">");
                for (int x = 0; x < this._xsize; x++)
break;
                {
case tileChest:
                    Console.Write(GetCellTile(x, y));
System.out.print("*");
                }
break;
 
};
                if (this._xsize <= xmax) Console.WriteLine();
}
            }
if (xsize < xmax) System.out.println();
        }
}
 
}
        public Direction RandomDirection()
        {
//and here's the one generating the whole map
            int dir = this.GetRand(0, 4);
public boolean createDungeon(int inx, int iny, int inobj){
            switch (dir)
if (inobj < 1) objects = 10;
            {
else objects = inobj;
                case 0:
                    return Direction.North;
//justera kartans storlek, om den ????r st????rre eller mindre ????n "gr????nserna"
                case 1:
//adjust the size of the map, if it's smaller or bigger than the limits
                    return Direction.East;
if (inx < 3) xsize = 3;
                case 2:
else if (inx > xmax) xsize = xmax;
                    return Direction.South;
else xsize = inx;
                case 3:
                    return Direction.West;
if (iny < 3) ysize = 3;
                default:
else if (iny > ymax) ysize = ymax;
                    throw new InvalidOperationException();
else ysize = iny;
            }
        }
System.out.println(msgXSize + xsize);
 
System.out.println(msgYSize + ysize);
        //and here's the one generating the whole map
System.out.println(msgMaxObjects + objects);
        public bool CreateDungeon(int inx, int iny, int inobj)
        {
//redefine the map var, so it's adjusted to our new map size
            this._objects = inobj < 1 ? 10 : inobj;
dungeon_map = new int[xsize * ysize];
 
            // adjust the size of the map, if it's smaller or bigger than the limits
//start with making the "standard stuff" on the map
            if (inx < 3) this._xsize = 3;
for (int y = 0; y < ysize; y++){
            else if (inx > xmax) this._xsize = xmax;
for (int x = 0; x < xsize; x++){
            else this._xsize = inx;
//ie, making the borders of unwalkable walls
 
if (y == 0) setCell(x, y, tileStoneWall);
            if (iny < 3) this._ysize = 3;
else if (y == ysize-1) setCell(x, y, tileStoneWall);
            else if (iny > ymax) this._ysize = ymax;
else if (x == 0) setCell(x, y, tileStoneWall);
            else this._ysize = iny;
else if (x == xsize-1) setCell(x, y, tileStoneWall);
 
            Console.WriteLine(MsgXSize + this._xsize);
//and fill the rest with dirt
            Console.WriteLine(MsgYSize + this._ysize);
else setCell(x, y, tileUnused);
            Console.WriteLine(MsgMaxObjects + this._objects);
}
 
}
            // redefine the map var, so it's adjusted to our new map size
            this._dungeonMap = new Tile[this._xsize * this._ysize];
/*******************************************************************************
 
And now the code of the random-map-generation-algorithm begins!
            // start with making the "standard stuff" on the map
*******************************************************************************/
            this.Initialize();
 
//start with making a room in the middle, which we can start building upon
            /*******************************************************************************
makeRoom(xsize/2, ysize/2, 8, 6, getRand(0,3)); //getrand saken f????r att slumpa fram riktning p?? rummet
            And now the code of the random-map-generation-algorithm begins!
            *******************************************************************************/
//keep count of the number of "objects" we've made
 
int currentFeatures = 1; //+1 for the first room we just made
            // start with making a room in the middle, which we can start building upon
            this.MakeRoom(this._xsize / 2, this._ysize / 2, 8, 6, RandomDirection()); // getrand saken f????r att slumpa fram riktning p?? rummet
//then we sart the main loop
 
for (int countingTries = 0; countingTries < 1000; countingTries++){
            // keep count of the number of "objects" we've made
//check if we've reached our quota
            int currentFeatures = 1; // +1 for the first room we just made
if (currentFeatures == objects){
 
break;
            // then we sart the main loop
}
            for (int countingTries = 0; countingTries < 1000; countingTries++)
            {
//start with a random wall
                // check if we've reached our quota
int newx = 0;
                if (currentFeatures == this._objects)
int xmod = 0;
                {
int newy = 0;
                    break;
int ymod = 0;
                }
int validTile = -1;
 
//1000 chances to find a suitable object (room or corridor)..
                // start with a random wall
//(yea, i know it's kinda ugly with a for-loop... -_-')
                int newx = 0;
for (int testing = 0; testing < 1000; testing++){
                int xmod = 0;
newx = getRand(1, xsize-1);
                int newy = 0;
newy = getRand(1, ysize-1);
                int ymod = 0;
validTile = -1;
                Direction? validTile = null;
//System.out.println("tempx: " + newx + "\ttempy: " + newy);
 
if (getCell(newx, newy) == tileDirtWall || getCell(newx, newy) == tileCorridor){
                // 1000 chances to find a suitable object (room or corridor)..
//check if we can reach the place
                for (int testing = 0; testing < 1000; testing++)
if (getCell(newx, newy+1) == tileDirtFloor || getCell(newx, newy+1) == tileCorridor){
                {
validTile = 0; //
                    newx = this.GetRand(1, this._xsize - 1);
xmod = 0;
                    newy = this.GetRand(1, this._ysize - 1);
ymod = -1;
 
}
                    if (GetCellType(newx, newy) == Tile.DirtWall || GetCellType(newx, newy) == Tile.Corridor)
else if (getCell(newx-1, newy) == tileDirtFloor || getCell(newx-1, newy) == tileCorridor){
                    {
validTile = 1; //
                        var surroundings = this.GetSurroundings(new PointI() { X = newx, Y = newy });
xmod = +1;
 
ymod = 0;
                        // check if we can reach the place
}
                        var canReach =
else if (getCell(newx, newy-1) == tileDirtFloor || getCell(newx, newy-1) == tileCorridor){
                            surroundings.FirstOrDefault(s => s.Item3 == Tile.Corridor || s.Item3 == Tile.DirtFloor);
validTile = 2; //
                        if (canReach == null)
xmod = 0;
                        {
ymod = +1;
                            continue;
}
                        }
else if (getCell(newx+1, newy) == tileDirtFloor || getCell(newx+1, newy) == tileCorridor){
                        validTile = canReach.Item2;
validTile = 3; //
                        switch (canReach.Item2)
xmod = -1;
                        {
ymod = 0;
                            case Direction.North:
}
                                xmod = 0;
                                ymod = -1;
//check that we haven't got another door nearby, so we won't get alot of openings besides
                                break;
//each other
                            case Direction.East:
if (validTile > -1){
                                xmod = 1;
if (getCell(newx, newy+1) == tileDoor) //north
                                ymod = 0;
validTile = -1;
                                break;
else if (getCell(newx-1, newy) == tileDoor)//east
                            case Direction.South:
validTile = -1;
                                xmod = 0;
else if (getCell(newx, newy-1) == tileDoor)//south
                                ymod = 1;
validTile = -1;
                                break;
else if (getCell(newx+1, newy) == tileDoor)//west
                            case Direction.West:
validTile = -1;
                                xmod = -1;
}
                                ymod = 0;
                                break;
//if we can, jump out of the loop and continue with the rest
                            default:
if (validTile > -1) break;
                                throw new InvalidOperationException();
}
                        }
}
 
if (validTile > -1){
 
//choose what to build now at our newly found place, and at what direction
                        // check that we haven't got another door nearby, so we won't get alot of openings besides
int feature = getRand(0, 100);
                        // each other
if (feature <= chanceRoom){ //a new room
 
if (makeRoom((newx+xmod), (newy+ymod), 8, 6, validTile)){
                        if (GetCellType(newx, newy + 1) == Tile.Door) // north
currentFeatures++; //add to our quota
                        {
                            validTile = null;
//then we mark the wall opening with a door
 
setCell(newx, newy, tileDoor);
                        }
 
//clean up infront of the door so we can reach it
                        else if (GetCellType(newx - 1, newy) == Tile.Door) // east
setCell((newx+xmod), (newy+ymod), tileDirtFloor);
                            validTile = null;
}
                        else if (GetCellType(newx, newy - 1) == Tile.Door) // south
}
                            validTile = null;
else if (feature >= chanceRoom){ //new corridor
                        else if (GetCellType(newx + 1, newy) == Tile.Door) // west
if (makeCorridor((newx+xmod), (newy+ymod), 6, validTile)){
                            validTile = null;
//same thing here, add to the quota and a door
                     
currentFeatures++;
 
                        // if we can, jump out of the loop and continue with the rest
setCell(newx, newy, tileDoor);
                        if (validTile.HasValue) break;
}
                    }
}
                }
}
 
}
                if (validTile.HasValue)
                {
                    // choose what to build now at our newly found place, and at what direction
/*******************************************************************************
                    int feature = this.GetRand(0, 100);
All done with the building, let's finish this one off
                    if (feature <= ChanceRoom)
*******************************************************************************/
                    { // a new room
                        if (this.MakeRoom(newx + xmod, newy + ymod, 8, 6, validTile.Value))
//sprinkle out the bonusstuff (stairs, chests etc.) over the map
                        {
int newx = 0;
                            currentFeatures++; // add to our quota
int newy = 0;
 
int ways = 0; //from how many directions we can reach the random spot from
                            // then we mark the wall opening with a door
int state = 0; //the state the loop is in, start with the stairs
                            this.SetCell(newx, newy, Tile.Door);
while (state != 10){
 
for (int testing = 0; testing < 1000; testing++){
                            // clean up infront of the door so we can reach it
newx = getRand(1, xsize-1);
                            this.SetCell(newx + xmod, newy + ymod, Tile.DirtFloor);
newy = getRand(1, ysize-2); //cheap bugfix, pulls down newy to 0<y<24, from 0<y<25
                        }
                    }
                    else if (feature >= ChanceRoom)
                    { // new corridor
                        if (this.MakeCorridor(newx + xmod, newy + ymod, 6, validTile.Value))
                        {
                            // same thing here, add to the quota and a door
                            currentFeatures++;
 
                            this.SetCell(newx, newy, Tile.Door);
                        }
                    }
                }
            }
   
            /*******************************************************************************
            All done with the building, let's finish this one off
            *******************************************************************************/
            AddSprinkles();
   
            // all done with the map generation, tell the user about it and finish
            Console.WriteLine(MsgNumObjects + currentFeatures);
   
            return true;
        }
   
        void Initialize()
        {
            for (int y = 0; y < this._ysize; y++)
            {
                for (int x = 0; x < this._xsize; x++)
                {
                    // ie, making the borders of unwalkable walls
                    if (y == 0 || y == this._ysize - 1 || x == 0 || x == this._xsize - 1)
                    {
                        this.SetCell(x, y, Tile.StoneWall);
                    }
                    else
                    {                        // and fill the rest with dirt
                        this.SetCell(x, y, Tile.Unused);
                    }
                }
            }
        }
   
        // setting a tile's type
        void SetCell(int x, int y, Tile celltype)
        {
            this._dungeonMap[x + this._xsize * y] = celltype;
        }
   
   
      }
    }


//System.out.println("x: " + newx + "\ty: " + newy);
ways = 4; //the lower the better
//check if we can reach the spot
if (getCell(newx, newy+1) == tileDirtFloor || getCell(newx, newy+1) == tileCorridor){
//north
if (getCell(newx, newy+1) != tileDoor)
ways--;
}
if (getCell(newx-1, newy) == tileDirtFloor || getCell(newx-1, newy) == tileCorridor){
//east
if (getCell(newx-1, newy) != tileDoor)
ways--;
}
if (getCell(newx, newy-1) == tileDirtFloor || getCell(newx, newy-1) == tileCorridor){
//south
if (getCell(newx, newy-1) != tileDoor)
ways--;
}
if (getCell(newx+1, newy) == tileDirtFloor || getCell(newx+1, newy) == tileCorridor){
//west
if (getCell(newx+1, newy) != tileDoor)
ways--;
}
if (state == 0){
if (ways == 0){
//we're in state 0, let's place a "upstairs" thing
setCell(newx, newy, tileUpStairs);
state = 1;
break;
}
}
else if (state == 1){
if (ways == 0){
//state 1, place a "downstairs"
setCell(newx, newy, tileDownStairs);
state = 10;
break;
}
}
}
}
//all done with the map generation, tell the user about it and finish
System.out.println(msgNumObjects + currentFeatures);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args){
//initial stuff used in making the map
int x = 80; int y = 25; int dungeon_objects = 0;
//convert a string to a int, if there's more then one arg
if (args.length >= 1)
dungeon_objects = Integer.parseInt(args[0]);
if (args.length >= 2)
x = Integer.parseInt(args[1]);
if (args.length >= 3)
y = Integer.parseInt(args[2]);
//create a new class of "dungen", so we can use all the goodies within it
dungen generator = new dungen();
//then we create a new dungeon map
if (generator.createDungeon(x, y, dungeon_objects)){
//always good to be able to see the results..
generator.showDungeon();
}
}
}
</source>
</source>
[[Category:Articles]]
 
 
[[Category:Developing]]

Latest revision as of 09:02, 2 June 2016

(Made by Solarnus)

This code is C#, not Java


     public class Dungeon
      {
    
        // misc. messages to print
        const string MsgXSize = "X size of dungeon: \t";
    
        const string MsgYSize = "Y size of dungeon: \t";
    
        const string MsgMaxObjects = "max # of objects: \t";
    
        const string MsgNumObjects = "# of objects made: \t";
    
        // max size of the map
        int xmax = 80; //columns
        int ymax = 25; //rows
    
        // size of the map
        int _xsize;
        int _ysize;
    
        // number of "objects" to generate on the map
        int _objects;
    
        // define the %chance to generate either a room or a corridor on the map
        // BTW, rooms are 1st priority so actually it's enough to just define the chance
        // of generating a room
        const int ChanceRoom = 75;
    
        // our map
        Tile[] _dungeonMap = { };
    
        readonly IRandomize _rnd;
    
        readonly Action<string> _logger;
    
    
        public Dungeon(IRandomize rnd, Action<string> logger)
        {
            _rnd = rnd;
            _logger = logger;
        }
        
        public int Corridors
        {
            get;
            private set;
        }
    
        public static bool IsWall(int x, int y, int xlen, int ylen, int xt, int yt, Direction d)
        {
            Func<int, int, int> a = GetFeatureLowerBound;
            
            Func<int, int, int> b = IsFeatureWallBound;
            switch (d)
            {
                case Direction.North:
                    return xt == a(x, xlen) || xt == b(x, xlen) || yt == y || yt == y - ylen + 1;
                case Direction.East:
                    return xt == x || xt == x + xlen - 1 || yt == a(y, ylen) || yt == b(y, ylen);
                case Direction.South:
                    return xt == a(x, xlen) || xt == b(x, xlen) || yt == y || yt == y + ylen - 1;
                case Direction.West:
                    return xt == x || xt == x - xlen + 1 || yt == a(y, ylen) || yt == b(y, ylen);
            }
            
            throw new InvalidOperationException();
        }
    
        public static int GetFeatureLowerBound(int c, int len)
        {
            return c - len / 2;
        }
    
        public static int IsFeatureWallBound(int c, int len)
        {
            return c + (len - 1) / 2;
        }
    
        public static int GetFeatureUpperBound(int c, int len)
        {
            return c + (len + 1) / 2;
        }
    
        public static IEnumerable<PointI> GetRoomPoints(int x, int y, int xlen, int ylen, Direction d)
        {
            // north and south share the same x strategy
            // east and west share the same y strategy
            Func<int, int, int> a = GetFeatureLowerBound;
            Func<int, int, int> b = GetFeatureUpperBound;
    
            switch (d)
            {
                case Direction.North:
                    for (var xt = a(x, xlen); xt < b(x, xlen); xt++) for (var yt = y; yt > y - ylen; yt--) yield return new PointI { X = xt, Y = yt };
                    break;
                case Direction.East:
                    for (var xt = x; xt < x + xlen; xt++) for (var yt = a(y, ylen); yt < b(y, ylen); yt++) yield return new PointI { X = xt, Y = yt };
                    break;
                case Direction.South:
                    for (var xt = a(x, xlen); xt < b(x, xlen); xt++) for (var yt = y; yt < y + ylen; yt++) yield return new PointI { X = xt, Y = yt };
                    break;
                case Direction.West:
                    for (var xt = x; xt > x - xlen; xt--) for (var yt = a(y, ylen); yt < b(y, ylen); yt++) yield return new PointI { X = xt, Y = yt };
                    break;
                default:
                    yield break;
            }
        }
    
        public Tile GetCellType(int x, int y)
        {
            try
            {
                return this._dungeonMap[x + this._xsize * y];
            }
            catch (IndexOutOfRangeException)
            {
                new { x, y }.Dump("exceptional");
                throw;
            }
        }
    
        public int GetRand(int min, int max)
        {
            return _rnd.Next(min, max);
        }
    
        public bool MakeCorridor(int x, int y, int length, Direction direction)
        {
            // define the dimensions of the corridor (er.. only the width and height..)
            int len = this.GetRand(2, length);
            const Tile Floor = Tile.Corridor;
    
            int xtemp;
            int ytemp = 0;
    
            switch (direction)
            {
                case Direction.North:
                    // north
                    // check if there's enough space for the corridor
                    // start with checking it's not out of the boundaries
                    if (x < 0 || x > this._xsize) return false;
                    xtemp = x;
    
                    // same thing here, to make sure it's not out of the boundaries
                    for (ytemp = y; ytemp > (y - len); ytemp--)
                    {
                        if (ytemp < 0 || ytemp > this._ysize) return false; // oh boho, it was!
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
    
                    // if we're still here, let's start building
                    Corridors++;
                    for (ytemp = y; ytemp > (y - len); ytemp--)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
    
                    break;
    
                case Direction.East:
                    // east
                    if (y < 0 || y > this._ysize) return false;
                    ytemp = y;
    
                    for (xtemp = x; xtemp < (x + len); xtemp++)
                    {
                        if (xtemp < 0 || xtemp > this._xsize) return false;
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
    
                    Corridors++;
                    for (xtemp = x; xtemp < (x + len); xtemp++)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
    
                    break;
    
                case Direction.South:
                    // south
                    if (x < 0 || x > this._xsize) return false;
                    xtemp = x;
                    
                    for (ytemp = y; ytemp < (y + len); ytemp++)
                    {
                        if (ytemp < 0 || ytemp > this._ysize) return false;
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
                    
                    Corridors++;
                    for (ytemp = y; ytemp < (y + len); ytemp++)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
                    
                    break;
                case Direction.West:
                    // west
                    if (ytemp < 0 || ytemp > this._ysize) return false;
                    ytemp = y;
                    
                    for (xtemp = x; xtemp > (x - len); xtemp--)
                    {
                        if (xtemp < 0 || xtemp > this._xsize) return false;
                        if (GetCellType(xtemp, ytemp) != Tile.Unused) return false;
                    }
                    
                    Corridors++;
                    for (xtemp = x; xtemp > (x - len); xtemp--)
                    {
                        this.SetCell(xtemp, ytemp, Floor);
                    }
                    
                    break;
            }

            // woot, we're still here! let's tell the other guys we're done!!
            return true;
        }

        public IEnumerable<Tuple<PointI, Direction>> GetSurroundingPoints(PointI v)
        {
            var points = new[]
                             {
                                 Tuple.Create(new PointI { X = v.X, Y = v.Y + 1 }, Direction.North),
                                 Tuple.Create(new PointI { X = v.X - 1, Y = v.Y }, Direction.East),
                                 Tuple.Create(new PointI { X = v.X , Y = v.Y-1 }, Direction.South),
                                 Tuple.Create(new PointI { X = v.X +1, Y = v.Y  }, Direction.West),
                                 
                             };
            return points.Where(p => InBounds(p.Item1));
        }

        public IEnumerable<Tuple<PointI, Direction, Tile>> GetSurroundings(PointI v)
        {
            return
                this.GetSurroundingPoints(v)
                    .Select(r => Tuple.Create(r.Item1, r.Item2, this.GetCellType(r.Item1.X, r.Item1.Y)));
        }

        public bool InBounds(int x, int y)
        {
            return x > 0 && x < this.xmax && y > 0 && y < this.ymax;
        }

        public bool InBounds(PointI v)
        {
            return this.InBounds(v.X, v.Y);
        }

        public bool MakeRoom(int x, int y, int xlength, int ylength, Direction direction)
        {
            // define the dimensions of the room, it should be at least 4x4 tiles (2x2 for walking on, the rest is walls)
            int xlen = this.GetRand(4, xlength);
            int ylen = this.GetRand(4, ylength);

            // the tile type it's going to be filled with
            const Tile Floor = Tile.DirtFloor;

            const Tile Wall = Tile.DirtWall;
            // choose the way it's pointing at

            var points = GetRoomPoints(x, y, xlen, ylen, direction).ToArray();

            // Check if there's enough space left for it
            if (
                points.Any(
                    s =>
                    s.Y < 0 || s.Y > this._ysize || s.X < 0 || s.X > this._xsize || this.GetCellType(s.X, s.Y) != Tile.Unused)) return false;
            _logger(
                      string.Format(
                          "Making room:int x={0}, int y={1}, int xlength={2}, int ylength={3}, int direction={4}",
                          x,
                          y,
                          xlength,
                          ylength,
                          direction));

            foreach (var p in points)
            {
                this.SetCell(p.X, p.Y, IsWall(x, y, xlen, ylen, p.X, p.Y, direction) ? Wall : Floor);
            }

            // yay, all done
            return true;
        }

        public Tile[] GetDungeon()
        {
            return this._dungeonMap;
        }

        public char GetCellTile(int x, int y)
        {
            switch (GetCellType(x, y))
            {
                case Tile.Unused:
                    return '';
                case Tile.DirtWall:
                    return '|';
                case Tile.DirtFloor:
                    return '_';
                case Tile.StoneWall:
                    return 'S';
                case Tile.Corridor:
                    return '#';
                case Tile.Door:
                    return 'D';
                case Tile.Upstairs:
                    return '+';
                case Tile.Downstairs:
                    return '-';
                case Tile.Chest:
                    return 'C';
                default:
                    throw new ArgumentOutOfRangeException("x,y");
            }
        }

        //used to print the map on the screen
        public void ShowDungeon()
        {
            for (int y = 0; y < this._ysize; y++)
            {
                for (int x = 0; x < this._xsize; x++)
                {
                    Console.Write(GetCellTile(x, y));
                }

                if (this._xsize <= xmax) Console.WriteLine();
            }
        }

        public Direction RandomDirection()
        {
            int dir = this.GetRand(0, 4);
            switch (dir)
            {
                case 0:
                    return Direction.North;
                case 1:
                    return Direction.East;
                case 2:
                    return Direction.South;
                case 3:
                    return Direction.West;
                default:
                    throw new InvalidOperationException();
            }
        }

        //and here's the one generating the whole map
        public bool CreateDungeon(int inx, int iny, int inobj)
        {
            this._objects = inobj < 1 ? 10 : inobj;

            // adjust the size of the map, if it's smaller or bigger than the limits
            if (inx < 3) this._xsize = 3;
            else if (inx > xmax) this._xsize = xmax;
            else this._xsize = inx;

            if (iny < 3) this._ysize = 3;
            else if (iny > ymax) this._ysize = ymax;
            else this._ysize = iny;

            Console.WriteLine(MsgXSize + this._xsize);
            Console.WriteLine(MsgYSize + this._ysize);
            Console.WriteLine(MsgMaxObjects + this._objects);

            // redefine the map var, so it's adjusted to our new map size
            this._dungeonMap = new Tile[this._xsize * this._ysize];

            // start with making the "standard stuff" on the map
            this.Initialize();

            /*******************************************************************************
            And now the code of the random-map-generation-algorithm begins!
            *******************************************************************************/

            // start with making a room in the middle, which we can start building upon
            this.MakeRoom(this._xsize / 2, this._ysize / 2, 8, 6, RandomDirection()); // getrand saken f????r att slumpa fram riktning p?? rummet

            // keep count of the number of "objects" we've made
            int currentFeatures = 1; // +1 for the first room we just made

            // then we sart the main loop
            for (int countingTries = 0; countingTries < 1000; countingTries++)
            {
                // check if we've reached our quota
                if (currentFeatures == this._objects)
                {
                    break;
                }

                // start with a random wall
                int newx = 0;
                int xmod = 0;
                int newy = 0;
                int ymod = 0;
                Direction? validTile = null;

                // 1000 chances to find a suitable object (room or corridor)..
                for (int testing = 0; testing < 1000; testing++)
                {
                    newx = this.GetRand(1, this._xsize - 1);
                    newy = this.GetRand(1, this._ysize - 1);

                    if (GetCellType(newx, newy) == Tile.DirtWall || GetCellType(newx, newy) == Tile.Corridor)
                    {
                        var surroundings = this.GetSurroundings(new PointI() { X = newx, Y = newy });

                        // check if we can reach the place
                        var canReach =
                            surroundings.FirstOrDefault(s => s.Item3 == Tile.Corridor || s.Item3 == Tile.DirtFloor);
                        if (canReach == null)
                        {
                            continue;
                        }
                        validTile = canReach.Item2;
                        switch (canReach.Item2)
                        {
                            case Direction.North:
                                xmod = 0;
                                ymod = -1;
                                break;
                            case Direction.East:
                                xmod = 1;
                                ymod = 0;
                                break;
                            case Direction.South:
                                xmod = 0;
                                ymod = 1;
                                break;
                            case Direction.West:
                                xmod = -1;
                                ymod = 0;
                                break;
                            default:
                                throw new InvalidOperationException();
                        }


                        // check that we haven't got another door nearby, so we won't get alot of openings besides
                        // each other

                        if (GetCellType(newx, newy + 1) == Tile.Door) // north
                        {
                            validTile = null;

                        }

                        else if (GetCellType(newx - 1, newy) == Tile.Door) // east
                            validTile = null;
                        else if (GetCellType(newx, newy - 1) == Tile.Door) // south
                            validTile = null;
                        else if (GetCellType(newx + 1, newy) == Tile.Door) // west
                            validTile = null;
                       

                        // if we can, jump out of the loop and continue with the rest
                        if (validTile.HasValue) break;
                    }
                }

                if (validTile.HasValue)
                {
                    // choose what to build now at our newly found place, and at what direction
                    int feature = this.GetRand(0, 100);
                    if (feature <= ChanceRoom)
                    { // a new room
                        if (this.MakeRoom(newx + xmod, newy + ymod, 8, 6, validTile.Value))
                        {
                            currentFeatures++; // add to our quota

                            // then we mark the wall opening with a door
                            this.SetCell(newx, newy, Tile.Door);

                            // clean up infront of the door so we can reach it
                            this.SetCell(newx + xmod, newy + ymod, Tile.DirtFloor);
                        }
                    }
                    else if (feature >= ChanceRoom)
                    { // new corridor
                        if (this.MakeCorridor(newx + xmod, newy + ymod, 6, validTile.Value))
                        {
                            // same thing here, add to the quota and a door
                            currentFeatures++;

                            this.SetCell(newx, newy, Tile.Door);
                        }
                    }
                }
            }
    
            /*******************************************************************************
            All done with the building, let's finish this one off
            *******************************************************************************/
            AddSprinkles();
    
            // all done with the map generation, tell the user about it and finish
            Console.WriteLine(MsgNumObjects + currentFeatures);
    
            return true;
        }
    
        void Initialize()
        {
            for (int y = 0; y < this._ysize; y++)
            {
                for (int x = 0; x < this._xsize; x++)
                {
                    // ie, making the borders of unwalkable walls
                    if (y == 0 || y == this._ysize - 1 || x == 0 || x == this._xsize - 1)
                    {
                        this.SetCell(x, y, Tile.StoneWall);
                    }
                    else
                    {                        // and fill the rest with dirt
                        this.SetCell(x, y, Tile.Unused);
                    }
                }
            }
        }
    
        // setting a tile's type
        void SetCell(int x, int y, Tile celltype)
        {
            this._dungeonMap[x + this._xsize * y] = celltype;
        }
    
    
      }
    }