Archive

Archive for the ‘Roguelike’ Category

Field of Vision using recursive shadow casting: C# 3.5 implementation

July 19th, 2009 No comments

Inspired by the article Field of Vision (FOV) using recursive shadowcasting – improved on the splendid website RougeBasin, I implemented the excellent algorithm in C Sharp 3.5. Below is a picture of the application:

fov

The black squares represent solid objects, the blue are what the player can see and white is the player.

The source code can be downloaded here.

What’s Going On?

The code is straight forwards enough.

The class game.cs contains the FOV algorithm. When instantiated the direction the player intends to move is passed to the method movePlayer and if the player has been able to move the event playerMoved is fired. The publicly exposed properties mapXLimit, mapYLimit, playerX, playerY and Sight when used in conjunction with the method getMapPoint can be used to draw the map and what the player can see.

There really isn’t that much to say about it as the code speaks for itself, but if you’re stuck with anything please add a comment and I’ll get back to you.

Using the application

The keys q,w,e,a,s,d,f,z,x and c control the player, and you can create solid objects with the left mouse button and remove objects with the right.

Categories: Roguelike Tags:

Irregular Shaped Rooms

July 15th, 2009 No comments

Here’s a link to an article I wrote for roguebasin on generating irregular shaped rooms.

I completely forgot about it until I came across a reference to it on http://pcg.wikidot.com and thought “this looks familiar”

Categories: Roguelike Tags:

Map Generator 3

May 14th, 2008 No comments

Behold (again)! An improved map generator for a roguelike game:

map2.png

It’s in C# and you can find the source code and a compiled executable here. As you can see in the above screenie you have two buttons called build and reset – if you can’t guess what they’re for you shouldn’t be reading this. You can also adjust properties using the grid, which will affect the appearance of the map produced.

Map generating is as follows:

  1. Create a room in the centre of the map, and randomly place a corridor that connects to it.
  2. Is the number of rooms created less than the property maxRoom?
    1. Yes – end
    2. No – go to step 3.
  3. Generate a number between 1 and 100:
    1. If less than 20 – attempt to build a new corridor on a randomly point on an existing corridor.
    2. If less than 40 – attempt to build a corridor on the end of a random corridor.
    3. If less than 60 – attempt to build a corridor on the randomly selected side of a room.
    4. If less than 80 – attempt to build a room off a randomly selected corridor. If a room is built increment room counter.
    5. If less than 100 – attempt to build a room off an end of a randomly selected corridor. If a room is built increment room counter.
  4. Go to step 2.

Note: All of the probabilities defined in step 3 can be changed in the properties grid.

There are many, many ways to generate a map / dungeon. The method described above is something that works for me, but can certainly be improved upon. If do can improve it, let me know and I’ll add it to the site.

Categories: Roguelike Tags:

Map Generator 2

May 10th, 2008 No comments

Behold! An extremely simple Rougelike map generator. You can find the C# source code and compiled executable here.

map1.png

It’s rather simple: the class called map contains a public method called build that will randomly build a map; there is a public two dimensional array called gameMap that contains the map; and to reset a public method called resetmap which resets the map.

The map building process can be summarised as follows:

  1. Build a single room in the centre of the map.
  2. Pick a random point on the map adjacent to closed cell.
  3. Decide upon a new feature to build.
  4. See if there is room to add the new feature.
    1. Yes – go to step 5.
    2. No – go back to step 2.
  5. Add the feature. If the feature is a room increment the room counter.
  6. Go back to step 3, until the room counter exceeds the maximum number of rooms required.

The form’s property grid contains 7 properties that are used in the generation of the map: their names are self evident, and changing them will change the characteristics of the final map.Obviously, this produces a primitive looking map: chaotic and sprawling, but it’s a nice effect and looks like the sort of thing a crazed horde of goblins would actually build…..

Categories: Roguelike Tags:

Map generator 1

December 18th, 2007 No comments

This is a simple map generating routine I developed in C#2.0. With a few tweaks it can produced “island” or “labyrinth ” type maps. I came across this algorithm whilst experimenting with different ways to generate maps for RogueLike games. You can download the C# source code and executable here.

How it works

Map generation is controlled by the following variables:

  1. map int[,] – a two dimensional array that holds the data.
  2. p (int) – close cell probability. Between 0 and 100.
  3. h (bool) – cell operation specifier.
  4. i (int) – counter.
  5. n (int) – number of cell’s neighbours.
  6. c (int) – examined cell’s closed neighbours. Between 0 and 8.

The procedure is as follows:

  1. Randomly choose a cell from map
  2. If the cell is open use a p to determine whether we close it.
  3. Get c
    1. h = true: If c > n close the cell, else open it.
    2. h = false: If c > n open the cell, else close it.
  4. Repeat steps1 – 3 i number of times.

Varying the above mentioned variables will produce maps of surprisingly different appearances.

Examples

The values x=100,y=100,p=45, h=true, n=4, i = 50000 produce an “island type” map.

default.png

The values x=100,y=100,p=45, h=false, n=4, i = 50000 produce a “labyrinth type” map.

variant1.png

x=100,y=100,p=55, h=true, n=4, i = 50000.
variant2.PNG

x=100,y=100,p=45, h=true, n=4, i = 85000.
variant3.PNG

x=100,y=100,p=45, h=false, n=2, i = 50000.
variant4.PNG

x=100,y=100,p=65, h=true, n=5, i = 50000.
variant5.PNG

x=100,y=100,p=75, h=true, n=5, i = 80000.
variant6.PNG

Categories: Roguelike Tags:

Line of Sight

December 12th, 2007 No comments

The most fundamental thing in a roguelike is line of sight (LOS), that is, the determination of what the player (and monsters) can see in their environment.

The method I am using for LOS is ray casting. This is where a line is drawn between two points using Bresenham’s Line Algorithm.

Calculating a sight zone

In a roguelike the area that a player can “see” is commonly within the boundaries of a circle which the player is the centre of. This application, called circle and pictured below, contains an example of this using an implementation of Bresenham’s alogrithm. The blue square represents the player, the white circle surrounding it is the area the player can see , the black blocks are barriers that the player cannot see through.

circle.PNG

It works by calculating all the points that lie within a circle of radius r around the player (x,y). If a point is within a circle whose centre point is the player the formula ((x1-x)^2 + (y1-y)^2) < r^2 is true.

Then a Bresenham line (BL) is drawn from the player to the location being examined, one point at a time. If that point is unoccupied it is added to a list and the next point is examined; if it is occupied it not added to the list and BL is abandoned, and so on. Upon completion or abandoning of a line, the points can be seen by the player and are drawn.

Using the application

Compile and run the application using C# express.

The spinbox in the top right hand corner is used to adjust the radius of the player sight.

Pressing the W key moves the player up, S moves the player down, A moves the player left and D moves the player right.

Right click anywhere within the form to add a barrier, and right click it again to remove it.

Observe how the player sight zone changes when you add and remove barriers and move around.

Categories: Roguelike Tags:

Bresenham’s line algorithm

July 9th, 2007 No comments

Bresenham’s line algorithm is the corner stone of any Roguelike game that uses raycasting as a means of determining what the player can see. It is used to draw an approximation of a straight line between two points. Wikipedia has a more detailed article.

You can download my simple app (bres) demonstrating this algorithm here. Bres contains a form representing a 25 by 25 grid, in which a Bresenham’s line is drawn between two points. To run it you’ll need to get hold of C# Express and compile the programme. I just don’t think having executables to download is a reassuring thing to have :)

Using the application

Compile and run the application using C# express.

Move the mouse around on the form and a line will be drawn between the start point (a blue square) and the current mouse position (a red square). Left click to place a new start point. Right click to place a barrier (a black block). To remove it, right click again. A barrier placed between the start and end point will stop a line being drawn between them; the line will begin at the start point and stop at the barrier (pictured below).

bres1.PNG

Categories: Roguelike Tags:

C# Roguelike

July 8th, 2007 No comments

This section is about my occasional attempts to develop a roguelike game in C#. Stuff will appear sporadically.

Categories: Roguelike Tags: