Month: October 2020

Thoughts on generating a dungeon level

Thoughts on generating a dungeon level

Dungeon Map
Map 19 from Paratime Designs Free B & W maps.

In my previous post, stage 1 of creating a RPG was “A Dungeon level generator. Create rooms and link them by corridors.“. Being quite retentive, I dug out my source code from the Quest dungeon generator. It was written in Turbo Pascal back in 1989-1990 and is about 1200 lines long.

The principle was slap down a few random sized rooms (both rectangular and circular in the original) then draw corridors from fixed points in the rooms with up to eight points in a room. Something like the map shown from Paratime Design.

You can go to town in the number of ways of generating a dungeon. When I was 16 i once spent a couple of weeks of my summer off-school time drawing a dungeon on an A2 piece of paper. It had something like 2000 rooms which is quite insane. I wished I’d kept it now; my magnum opus!

Another way I’d considered is draw a grid on the area. My original dungeon area was 150 x 150 points in size. Mind you Turbo Pascal was 16-bit software and no data structure could be bigger than 65536 bytes so the limit would have been 256 x 256.  If you draw a grid say at 8 x 8 intervals then when you put a rectangular room  down the intersection with the grid provides door points.  My original rooms were either small (6-18) or large (10-40) points.

Once the rooms were in place, corridors were drawn until all rooms were linkable. Corridors were drawn out from a door point until they hit another corridor or door point. By building up a list of rooms connected to other rooms, and removing duplicate corridors (rooms directly linked to another room more than once), a minimum dungeon level was built.

After that traps, treasures and monsters were added and an entry point in and out of each level to the one above or below. If you are feeling fancy then you can add a special room or two on each level such as temples, arenas and such like.

If you are short of imagination you could buy the D&D Dungeon Master’s Guide 5th Edition. Appendix A has a very comprehensive guide to creating a random dungeon and stocking it with traps, obstacles, furnishings and monsters.

How to implement a Roguelike

How to implement a Roguelike

Roguelike dungeon
From Wikimedia

A roguelike is a character based fantasy game. By character I mean @^! not an individual as such! A question on the C SubReddit had asked about Project ideas for simple applications and someone had suggested a Roguelike. It’s not a bad idea but probably quite a bit more than just a simple project.

So I suggested breaking it down into stages. Here’s what I said.

Rather than a roguelike in one go (that’s a actually quite a bit of code) so do it in these stages.

  1. A Dungeon level generator. Create rooms and link them by corridors.

  2. Generate a bunch of levels – link them via randomly stairs, pits, transporters.

  3. Add random monsters and treasures in rooms.

  4. Implement a moving player able to navigate through the levels.

  5. Add combat. Weapons, range weapons, spell casting. Add monster hit points.

  6. Turn it into a polished game. Add everything else needed. Permadeath, collecting treasures. Moving monsters.

  7. (Optional) Make it multiplayer and allow PvP.

PvP means allow Player v Player combat. Doing it multiplayer is actually quite a lot of work which is why I made it an option. Rogues are often created using simple ASCII chars for monsters and treasures. Some programmers have used graphics and there are plenty of free graphics sets for 8 x 8 or 16 x 16 pixel sized monsters etc. like the Kenney.nl microrogue set. Shown in action below.

Kenney.nl micro rogue set in action

I am tempted to make this the 3rd game idea for the Raspi game book. 30 years ago I created a multiplayer postal game Quest that is still running albeit on the web not by post. That included a dungeon generator. It wasn’t in C but it’s easy enough to translate Turbo Pascal to C.

Single file libraries

Single file libraries

open book lot
Library image from Unsplash.com

Most days I scan the web for relevant or interesting stuff and today I found a real treasure. A website where they have curated a list of C and C++ single file libraries. That is libraries that are not multiple files but mostly one. I say mostly because there are a few with 2 or 3 files.

The bulk are in C++ but there are a large number of C/C++ and quite a number of C. All have their licensing; these are open source but licences vary a bit.

It’s worth a trawl through the List. For instance there’s an interpreter for a BASIC dialect scripting language,

Tutorial six on pointers added

Tutorial six on pointers added

Pointers
Image by Please Don’t sell My Artwork AS IS from Pixabay

Once you ‘get’ pointers they are very easy to use. I think I always got them easily because long ago I used to do game programming in assembly language. When you are accessing blocks of ram indirectly through registers in assembly, then the concept of a pointer in C/C++ comes fairly naturally.

But really, all a pointer is, is a variable that holds an address. That address can be one of several things. It could be to a string of characters, an int or float variable, a struct, an array. In fact it can be anything that can exist in memory.

There are some limitations. It’s not good practice to use a pointer to access the underlying binary of your program, assuming that you can locate it. Plus chances are that code will be in memory that you cannot write to. Data however will let you write to it and pointers make your program far more flexible than without.

Tutorial six on pointers has been published. I’ll publish another one to follow it.

Restarting the Raspberry PI C Games tutorials

Restarting the Raspberry PI C Games tutorials

Raspberry Pi 4I believe that the Raspberry PI, especially the 4B is a great and very low cost machine for not only running games but for developing them as well. Of course, if you have a PC, Linux or Mac then you can use that as a development machine but if you haven’t, it costs less than £100 (when you count the system, case, cables, SD-Card) to get up and running.

So I’m reworking my original eBook for the Raspberry Pi, using software running on the Pi and developing a 2nd ebook. Along the way I’ll publish longer excerpts from it here. Probably one a week.

 

 

Interesting looking Game Handheld device- Odroid-Go

Interesting looking Game Handheld device- Odroid-Go

Odroid-GoThe Odroid-go Game kit looks like a gameboy. It has a LCD, game controllers and the case. It has a CPU that runs between 80 and 240 MHZ, 4MB RAM, WiFi, Bluetooh, a Micro-SD slot, a rechargeable battery – charged through a Micro USB, a built-in speaker and a 320 x 240 LCD. That may not sound very much but all the CBM-64 games ran on that size of screen and it only had 64 KB of RAM.

There’s more information on the manufacturers website (it’s in English though it is Chinese made) and all the useful information is on a Wiki with info on emulators, downloadable games and how to write programs for it using the Arduino programming language which is very much like C/C++. You have to install Arduino for ESP32 first on a Pc (Windows, Linux or Mac) then can you write programs and run them on your Odroid-Go.

There’s a bit of assembling but the website includes instructions with photos. A quick search on Youtube found a video showing how to assemble it.

This is an example of Arduino code, taken from their online reference manual. The Ardunio has been around for several years so there is a lot of code around for it.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}
More on Shuffledness

More on Shuffledness

DEck of playing cards
Dowload from Creazilla.com

So I’ve updated my shuffledness calculation. This is a bit of a work in progress so probably isn’t the last version.  One of the things I’m interested in is what is the optimal number of swaps to get a good shuffled deck of cards. The shuffle algorithm picks two random indexes in the array 0-51 and then swaps the contents.

Obviously doing this 10 times would only shuffle at most 20 cards and probably fewer as there’s no checks for repeats. The only check is that both indexes (indices?) are not the same. So I want to experiment and see what an optimal value for numTries should be..

void ShuffleDeck(int numtries) {
	int firstIndex, secondIndex;
	for (int i = 0; i < numtries; i++) {
		do {
			firstIndex = rand() % 52;
			secondIndex = rand() % 52;
		}
		while (firstIndex == secondIndex);
		int value = deck[firstIndex];
		deck[firstIndex] = deck[secondIndex];
		deck[secondIndex] = value;
	}
}

Anyway, I’ve added a CalcDistance() function. This looks at each card and calculates how far it has moved from it’s original position.  It then divides this by the maximum distance it could have moved to get a move “factor”. These are summed up and divided by 52 to give an average move factor. That’s your CalcDistance() function.

float CalcDistance() {
	float distance = 0.0f;
	for (int i = 0; i < 52; i++) {
		float distanceDiv = (i < 26) ? 51 - i : i-1;
		//printf("%i: %3.0f\n", i, distanceDiv);
		distance += (abs(deck[i] - i) / distanceDiv);
	}
	return distance/52.0f;
}

The only thing odd about this function is the distanceDiv value which starts at 51, decreases to 25 then increases to 50. The printf let me check that was correct.

This is the current state of the program which I’ve listed fully below. Enjoy! It’s 63 lines long.

// shuffledness.c : This measures how shuffled a deck of cards is
// cards are hjeld as values 0-51 in a 52 int aaray.

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int deck[52];
time_t t;

// Order the cards 0-51. 0 = Acew Hards,13 = Ace Clubs,26 = Ace of Diamonds and 51 = King of Spades
void InitDeck() {
	for (int i = 0; i < 52; i++) {
		deck[i] = i;
	}
}

// Works on glovbal array deck and calculates a value for how displayed each card is
float CalcDisorder() {
	int total=0;
	for (int i = 1; i < 52; i++) {
		total += abs(deck[i] - deck[i - 1]);
	}
	printf("Total = %d\n", total);
	return total / (52.0f * 26.0f);
}

// Sum up distance card moved/max distance
float CalcDistance() {
	float distance = 0.0f;
	for (int i = 0; i < 52; i++) {
		float distanceDiv = (i < 26) ? 51 - i : i-1;
		//printf("%i: %3.0f\n", i, distanceDiv);
		distance += (abs(deck[i] - i) / distanceDiv);
	}
	return distance/52.0f;
}

// Shuffle a deck by swapping two random cards a specified number of times
void ShuffleDeck(int numtries) {
	int firstIndex, secondIndex;
	for (int i = 0; i < numtries; i++) {
		do {
			firstIndex = rand() % 52;
			secondIndex = rand() % 52;
		}
		while (firstIndex == secondIndex);
		int value = deck[firstIndex];
		deck[firstIndex] = deck[secondIndex];
		deck[secondIndex] = value;
	}
}

int main() {
	/* Intializes random number generator */
	srand((unsigned)time(&t));
	for (int i = 0; i < 25; i++) {
		InitDeck();
		ShuffleDeck(1000);
		printf("CalcDisorder() ==%f x %f = %f\n", CalcDisorder(),CalcDistance(),(double)CalcDisorder() * CalcDistance());
	}
	return 0;
}

Because I’m multiplying the two factors (disorder and Distance, the final value is never getting much above 0.35 and I’d prefer it to be in the range 0-0.999. Suggestions welcomed.

.

Compiling and linking C

Compiling and linking C

Code
Image by fancycrave1 from Pixabay

So, if you are new to programming Compiling and linking can seem like magic. The compiler transforms C source code files into obj files. The actual format is different depending on the CPU and Operating system.  Linking is even more magic because it takes a bunch of obj files and produces a single executable.

There’s actual more magic going on than my simplistic explanation. The compiler might be doing optimisation (generally in release mode but not debug). That makes debug compiles faster which is more desirable.

Another blog called Hack the developer has published an article with a lot more detail about compiling and linking. If you want to get a better understanding then this is a petty good piece. It uses Linux, GCC and Clang as examples. It also goes into more depth on the layout of obj files.

How to visit many many places in one go

How to visit many many places in one go

green points
Image by Gerd Altmann from Pixabay

No this isn’t turning into a travellog.  Imagine a huge square. It has 33,554,432 points along an edge. The entire square would thus have 1,125,899,906,842,642 points in it. That’s 250 or 3210 points. (Yes those two numbers are the same!)

Now how about contriving an algorithm that will pick a random point in this square and keep picking each point but never more than once? Eventually it will have picked every point.  If it could visit a million points every seconds, it would take something like 35.7 years to visit each and every point.

So assuming you could create an algorithm, how large would it be? How about three lines?

        public static ulong Nextulong()
        {
            ulong result = ((aUVal * ULast) + bUVal) % aUVal;
            ULast = result;
            return result;
        }

That’s in C#. The values for aUVal and bUval are

        const ulong aUVal = 1125899906842597;
        const ulong bUVal = 1051122009542795;

A uLong is just an unsigned long. That’s a 64 bit unsinged int.
This is an example of a Linear Congruential Generator BTW.

Posted in C#
How to count lines of code in Visual Studio

How to count lines of code in Visual Studio

Calucating Code metrics in Visual StudioUntil this morning, I didn’t realise that Visual Studio (even the free Community Edition) has this built in. I was curious to see how much code I’d written so far for my game processing engine.  Development comes in leaps and bounds during evenings and weekends.

Visual Studio has Code Metrics in it. You can do this for any project that builds correctly. To show this you have to do View => Other Windows => Code Metrics Results. That displays the window. Then you have to go on the Analyze Window, and click Calculate Code Metrics.

Lines of code

However you might want to take these with a pinch of salt. As I understand it, Lines of Source Code is calculated from the il code output from the compiler. The last column (Lines of executable code) suggests that most of my program is comments which is wrong! There is however a fair amount of data in the form of Constructor initializations, and one file of 1290 lines has over 1,000 such initializations.  You can read what the other columns mean and everything you ever wanted to know about code metrics on the Microsoft site.

Out of curiosity, I manually counted the number of lines in this project. There are 15 files and they added up to 3700 lines but that includes comments and blank lines.  A quick search for // found 119 which probably means I should improve my commenting. Blank lines is probably something similar. So we’re talking 3700-1000 (constructor initializers)-119 (comments) -119 (blank lines) = 2462 lines of code so far. That’s working tested code mind you.