Tutorial on Dungeon Generation in Objective-C

So I am getting a Cellular Automator (CA) generator ready for this years 7DRL and hoping to create the same game in iOS and HTML5 – I am sure I am biting too much off. However, I have a basic implementation of a Dungeon generator and detailed how to create it below in X-Code using Objective-C. It’s a very simple implementation – so feel free to comment if you think we can clean the code up!!!!

In terms of CA there are some great references to get you going:

http://pcg.wikidot.com/pcg-algorithm:cellular-automata

http://www.futuredatalab.com/proceduraldungeon/

http://csharpcodewhisperer.blogspot.com.au/2013/07/Rouge-like-dungeon-generation.html

For this tutorial the dungeon is represented by 0’s as Rock and 1’s as  Path (or moveable space).

This is the output so far:

Screen Shot 2014-02-16 at 1.19.12 AM

So its a pretty simple set of steps to get us going.

Firstly create a OSX->Application->Command LineTool->Foundation project in Xcode

Then add a class called Dungeon

The header for Dungeon should look like this

#import <Foundation/Foundation.h>
 @interface Dungeon : NSObject
{
    int map[100][100];  //max size of dungeon
    int maxRows;        //size of this dungeon
    int maxCols;        //size of this dungeon
}
- (id)initWithRows:(int)rows AndCol:(int)cols;   
-(void)printMap;
@end

The implementation for this should look like

#import "Dungeon.h"

@implementation Dungeon
- (id)initWithRows:(int)rows AndCol:(int)cols
{
    self = [super init];
    if (self) {
        [self initMap];
        maxRows = rows;
        maxCols = cols;
        [self createCellularAutomatorDungeon:maxRows ForCols:maxCols];
    }

    return self;
}

//set all the map to rock
-(void)initMap
{
    for(int i=0;i<maxRows;i++)
        for(int j=0;j<maxCols;j++)
            map[i][j] = 0;
}
 //Loop through the 2 dim array and add space (not rock) if the wall has a specific minimum neighbourCount
- (void)createCellularAutomatorCellsForNeighbourCount:(int)neighbourCount
{
    //first pass CA
    for(int i=0;i<maxRows;i++)
    {
        for(int j=0;j<maxCols;j++)
        {
            if (map[i][j] == 0 && ([self getNeighboursForRow:i ForCol:j]>=neighbourCount))
                map[i][j] = 1;
        }
    }
}

//use a classic CA algorithm 
-(void)createCellularAutomatorDungeon:(int)rows ForCols:(int)cols
{
    //randomly initialize wih a few random spaces in the rock
    for(int i=1;i<maxRows;i++)
        for(int j=1;j<maxCols;j++)
            if (arc4random()%4==1)
                map[i][j] = 1;

    [self createCellularAutomatorCellsForNeighbourCount:4];//first pass
    [self createCellularAutomatorCellsForNeighbourCount:5];//second pass
}

//Count of the number of neighbours for this point
-(int)getNeighboursForRow:(int)row ForCol:(int)col
{
    int n = 0;

    //top neighbours
    if (row>0 && col>0)
    {
        for(int i=col-1;i<col+2;i++)
        {
            if (map[row-1][i]==1)
                n++;
        }
    }

    //middle neighbour left
    if (col>0 && col<maxCols)
        if (map[row][col-1]==1)
            n++;

    //middle neighbour right
    if (col>0 && col<maxCols)
        if (map[row][col+1]==1)
            n++;

    //bottom neighbours
    if (row<maxRows && col<maxCols)
    {
        for(int i=col-1;i<col+2;i++)
        {
            if (map[row+1][i]==1)
            {
                n++;
            }
        }
    }
    return n;
}

-(BOOL)ifValidCoordinateForRow:(int)row ForCol:(int)col
{
    return (map[col][row]==0 && row>0 && col > 0 && row<maxRows && col<maxCols);
}

-(void)printMap
{
    for(int i=0;i<maxRows;i++)
    {
        for(int j=0;j<maxCols;j++)
            printf("%u", map[i][j] );
        printf("\n");
    }
}

And make sure that main.m looks like

#import <Foundation/Foundation.h>
#import "Dungeon.h"
 int main(int argc, const char * argv[])
{
    @autoreleasepool {
        // insert code here...
        NSLog(@"Basic Dungeon CA Algorithm");
        Dungeon *dungeon  =[[Dungeon alloc]initWithRows:30 AndCol:50];
        [dungeon printMap];
            }
    return 0;

}

Ok so if you look at the code above in dungeon.m you should get a pretty good idea of how we are doing the generation of the dungeon using a CA algorithm. Read the comments as I have tried to explain anything a little unusual. If anything doesn’t make sense – leave a comment and I’ll get back to you.

Happy coding.

iPhone roguelike design doco for my University assignment

OK slightly off topic – but this is my first assignment (presentation) for my iOS rogue like/RPG game

http://dungeonbard.com/2012/09/14/powerpoint-presentation-on-dungeonbard-i-gave-a-few-days-ago-at-university/
Dom

 

iPhone Project 1

Background

  • This is the first iPhone app I wrote last year
  • There is not a lot of code!!!
  • Best way to do this is just copy the code in XCode as needed and follow the simple instructions below about creating the UI in Interface Builder

Requirements

  • Provide effective light from the iPhone in Darkness
  • On/Off Switch
  • No Fade Out/Dim
Specification
  • See screen below.

  • Label is White when torch is on (Yes this is a very simple torch!)
  • Label is black when torch is off
  • On Application start torch is off
  • Off/On button toggles the light
Test
  • None required at this stage (OK so my objective C needs to get improved before I start building unit tests first). I promise it will happen …. yeah yeah.

Code (Please Note was built with XCODE 3.0.1)

  • iPhone_TorchViewController.h

//  iPhone_TorchViewController.h
//  iPhone Torch
//
//  Created by Dom Millar on 31/08/09.
//  Copyright DOMSCODE 2009. All rights reserved.
//
#import
@interface iPhone_TorchViewController : UIViewController {
IBOutlet UILabel *torchlabel;
}
@property (retain,nonatomic) UILabel *torchlabel;
-(IBAction)buttonPressed:(id)sender;
@end
  • iPhone_TorchViewController.m (Important Bits)

#import “iPhone_TorchViewController.h”

@implementation iPhone_TorchViewController
@synthesize torchlabel;
-(IBAction)buttonPressed:(id)sender
{
NSString *buttonTitle = [sender titleForState:UIControlStateNormal];
if([buttonTitle compare:@”On”] == NSOrderedSame)
{
[torchlabel setBackgroundColor:[UIColor whiteColor]];
}
else
{
[torchlabel setBackgroundColor:[UIColor blackColor]];
}
}

– (void)dealloc {
[torchlabel release];
[super dealloc];
}
Ok so now to hook up to the UI build the code in XCode then open the XIB File. Add the UI elements to the main view like in the above screenshot (1 label with background black, 2 buttons). Then hooking up the outlets and action is easy. Two steps in fact:
1. Option click on the File Owner and drag from the label item to the actual big label on the view.
2. Click on each of the buttons and bring up the button connections. Click on the Touch Up Inside connection and drag that to the File Owner – selecting the button pressed action. Make sure you do this for both buttons.


Issues
  • Should make it a bit more funky (FlashLight – Version 2)
  • Using the title of the buttons to determine which one pressed is a bit dodgy – will see if we can improve this

Summary

Ok so this was an extremely simple applIcation and no test driven development (Curse you Dom) but was achieved in just over an hour and considering this is my first iPhone app – I am pretty happy. In terms of the process, it was all fairly basic but as we consider this series of Projects you’ll see things get more complex.

Dom

Upgrading from Leopard to Snow Leopard – for XCode and Monotouch

I just thought it was worthwhile documenting the upgrade process I had to go through upgrading my mac from Leopard to Snow Leopard, as it was a bit of a pain.

My starting point was:

Leopard
Monotouch 1.0
MonoDevelop 2.4
iPhone SDK 3.0.1

How I upgraded:

SnowLeopard Upgrade
SDK for Leopard 3.1.2 –  Note: I tried Monotouch at this point and it failed so then embarked on the following:
MonoFramework 
MonoDevelop 2.2 (beta)
MonoTouch 1.1
Then Discovered 3.1.2 was avail
SDK for Leopard 3.1.2 (Note for young players encountered some problems because the simulator was running and was kept stopping the install – closed he simulator and all fixed)

Then all worked fine!!!!

Hope this helps
Dom