Search Unity

Roguelike Algorithm

Discussion in 'Scripting' started by cadw96, Apr 22, 2014.

  1. cadw96

    cadw96

    Joined:
    Oct 27, 2013
    Posts:
    20
    Hi,

    I am working on a rpg game. I have been able to randomly generate an area, but I'd like to make rooms and corridors, but I do not understand how the Roguelike algorithm works in Unity after a lot of research. Could someone explain it to me?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    "the roguelike algorithm" ... there are many ways of approaching building a dungeon. Which one have you researched? maybe narrow it down to "I don't understand this bit of this algorithm" rather than asking such an open ended question.
     
  3. cadw96

    cadw96

    Joined:
    Oct 27, 2013
    Posts:
    20
    You're right! :) I have read about the standard algorithm used in the game Rogue from the 80's, this algorith is nowadays called the Roguelike algorithm. I have read a lot about how it works on these two sites and I totally understand what they mean:

    http://kuoi.org/~kamikaze/GameDesign/art07_rogue_dungeon.php
    http://www.roguebasin.com/index.php?title=Dungeon-Building_Algorithm

    My problem however is that I do not know how I should program this in C#. This is the C++ example I read, but it's not really helping :/

    http://www.roguebasin.com/index.php?title=C++_Example_of_Dungeon-Building_Algorithm
     
  4. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
  5. cadw96

    cadw96

    Joined:
    Oct 27, 2013
    Posts:
    20
  6. cadw96

    cadw96

    Joined:
    Oct 27, 2013
    Posts:
    20
    These are the steps you have to make for the algorithm and I do not how to code these (not even step 1 :p) in Unity in C#:

    - Fill the whole map with solid earth
    - Dig out a single room in the centre of the map
    - Pick a wall of any room
    - Decide upon a new feature to build
    - See if there is room to add the new feature through the chosen wall
    - If yes, continue. If no, go back to step 3
    - Add the feature through the chosen wall
    - Go back to step 3, until the dungeon is complete
    - Add the up and down staircases at random points in map
    - Finally, sprinkle some monsters and items liberally over dungeon
     
  7. cadw96

    cadw96

    Joined:
    Oct 27, 2013
    Posts:
    20
    I have step one working (was not hard at all, but how can I create the rooms:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Map : MonoBehaviour {
    5.    
    6.     public bool generated = false;
    7.     public float screenWidth;
    8.     public float screenHeight;
    9.     public int width = 20;
    10.     public int height = 20;
    11.     public UnityEngine.Texture Blue;
    12.     public UnityEngine.Texture Border;
    13.    
    14.     // Use this for initialization
    15.     void Start () {
    16.         screenWidth = Screen.width;
    17.         screenHeight = Screen.height;
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {       
    22.        
    23.         if (generated == false) {
    24.             for (int y = 0; y < height; y++) {
    25.                 GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
    26.                 cube.transform.position = new Vector3 (y + 1, 0, 0);
    27.                 cube.name = "MapTiles";
    28.                 cube.renderer.material.mainTexture = Blue;
    29.                 for (int x = 0; x < width; x++)
    30.                 {                  
    31.                     GameObject cubetwee = GameObject.CreatePrimitive (PrimitiveType.Cube);
    32.                     cubetwee.transform.position = new Vector3 (y + 1, x + 1, 0);
    33.                     cubetwee.name = "Maptiles";
    34.                     cubetwee.renderer.material.mainTexture = Blue;
    35.                 }
    36.             }
    37.             generated = true;
    38.         }
    39.     }
    40. }
     
  8. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    Okay, so you're filling an area with cubes. I think it would be useful to put those cubes into a 2 dimensional array, so you can access them if you're erasing them.

    Code (csharp):
    1. public GameObject [] squares;
    2.  
    3. ...
    4.  
    5. squares = new GameObject [width , height];
    6.  
     
  9. cadw96

    cadw96

    Joined:
    Oct 27, 2013
    Posts:
    20
    How should I do that? I made this code and it generates some rooms:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Map : MonoBehaviour {
    5.    
    6.     public bool generated = false;
    7.     public bool lakegenerated = false;
    8.     public float screenWidth;
    9.     public float screenHeight;
    10.     public int width = 20;
    11.     public int height = 20;
    12.     public UnityEngine.Texture Blue;
    13.     public UnityEngine.Texture Border;
    14.     public Transform Grass;
    15.     public Transform Water;
    16.     public int LakeAmount;
    17.     public int LakeWidth;
    18.     public int LakeHeight;
    19.     public int LakePositionX;
    20.     public int LakePositionY;
    21.    
    22.     // Use this for initialization
    23.     void Start () {
    24.         screenWidth = Screen.width;
    25.         screenHeight = Screen.height;
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {       
    30.        
    31.         if (generated == false) {
    32.             for (int y = 0; y < height; y++) {
    33.  
    34.                 {  
    35.                     for (int x = -1; x < width; x++){
    36.                         Instantiate(Grass, new Vector3 (y , x + 1, 0), Quaternion.identity);
    37.                     }
    38.                 }
    39.             }
    40.             generated = true;
    41.         }
    42.         if (lakegenerated == false)
    43.         {
    44.             for (int amount = 0; amount < LakeAmount; amount++)
    45.             {
    46.                 LakeWidth = Random.Range (3, 15);
    47.                 LakeHeight = Random.Range (3, 15);
    48.                 LakePositionX = Random.Range (0, width - LakeWidth);
    49.                 LakePositionY = Random.Range (0, height - LakeHeight);
    50.                
    51.                 for (int heightlake = 0; heightlake < LakeHeight  (heightlake + LakePositionY) < height; heightlake++)
    52.                 {
    53.                     for (int widthlake = 0; widthlake < LakeWidth  (widthlake + LakePositionX) < width; widthlake ++){
    54.                         Instantiate(Water, new Vector3 (LakePositionX + heightlake , LakePositionY + widthlake + 1, 0), Quaternion.identity);
    55.                     }
    56.                 }
    57.             }
    58.             lakegenerated = true;
    59.         }
    60.     }
    61. }
    62.  
    63.  
     
    Last edited: Apr 23, 2014