Month: November 2020

More thoughts on the design of Rogue like

More thoughts on the design of Rogue like

Unicorn hackSometimes I walk up around 3:00 AM and my mind is abuzz with things like this. Last night was one such night. The first thought was I should stop calling it a Roguelike. There’s a certain set of conventions with those and I don’t want to be limited by that.

For instance here’s an article about things you should or shouldn’t do in designing Roguelikes.

The game (for the next Ebook) is not going to be an all singing all dancing version but will be an MVP in the parlance. That’s Minimum Viable Product,

So if I’m going to divert away from the ‘standard’ then it needs a name. Something evocative like Dungeon Trawler but shorter more catchy.  So I’m going with V.O.R. (Vaults of Ruin) or maybe just Vor,

So onto design aspects. Here’s a list of the features:

  • 10-15 different Monster types with differing melee, range weapons and magic. Can have packs of them, not just one. There is only one player. Should be fun!
  • Simple set of castable spells using a Mana value. It is used up casting spells but regenerates slowly as you move. It can be replenished quicker by consuming potions.
  • No food or water but the character has an energy rating. This is depleted by movement and fighting. Rest or sleep oe rating food replenishes it but I don’t want it to be a major thing. I’m not having deep dungeons with 25 or more levels, but 12 levels maximum.
  • Weapons are sword, dagger or bow. Can switch between them.
  • A few powerful magical treasures.
  • Turn based not real-time. Asteroids is real-time but movement will be restricted to so many squares for you and monsters. You probably won’t be able to outrun the faster moving monsters.
  • Limited visibility can make things interesting. It means the game has to calculate what squares are visible each move.

I was tempted to do away with grid movement; for instance in the Asteroids game, they can move at any angle and velocity and are not tied to a grid. But that might be a bit too complicated so movement will be grid limited and Up/Down, Left/Right and maybe diagonal.

The screenshot is from an open source (.NET Core/C#) web rogue game called Unicornhack.

 

 

Hyper-V VMs are not all the same

Hyper-V VMs are not all the same

Hyper-V Ubuntu installOne of the big problems with Hyper-V and Ubuntu in particular is the clipboard or lack of it. I had 18.04 LTS installed with an X Org RDP login. This worked perfectly and I could have a full screen in my Monitor and could copy/paste.  Don’t underestimate copy/paste.

It’s a real PITA if you have to use say WinSCP to copy files over. I think WinSCP is excellent BTW but the amount of labour saving that copy/paste has done since some genius thought it up is immeasurable. That and allowing the full screen of the monitor are two highly important things.

Sadly the 20.04 LTS didn’t seem to allow it. Copy/paste didn’t work between my Windows PC (host) and Ubuntu (guest). There’s nothing worse than losing a feature you’ve grown fond of.

If you follow these instructions for creating a Hyper-V 18.04, you get the screen size popup but not with 20.04 LTS. For that you have to follow these instructions!

It’s things like this that suggest why Linux Desktop has never been that successful. You can waste many hours getting simple things working and sometimes like Copy/Paste they break between versions. And this is with Ubuntu, probably the biggest and best known and supported Distro.

 

And my Hyper-V Raspi error

And my Hyper-V Raspi error

Scary apt messageSeems to be with Visual Studio Code. I said yesterday that it had got into a funny state. Well I created a new VM and installed the Raspberry Pi OS that runs in a VM and after it updated tried installing VS Code on it.

It would have had the same problem had I let it. The problem is it when you install VS Code, it has some unmet dependencies and in fixing them it wants to remove 8 essential packages and this breaks apt which then gets in a funny state.

No one wants to see this scary message! So I think I may have to use the Code.headmelted.com version on Hyper-V for a while and see if this gets resolved with the next update of VS Code. Ah the jots of software development…

 

When Raspberry Pi Goes wrong

When Raspberry Pi Goes wrong

Raspberry Pi TuxIt happened to me today, not once but twice.  First the Hyper-V version of Raspberry PI OS I have installed (handy for screenshots) didn’t update properly.  Apt got itself in a right state and attempts to fix broken packages on APT just made things worse. In the end, I fetched another copy from this page and installed that in Hyper-V and deleted the first one. I wonder if Raspberry PI OS will ever make it into the Microsoft Store as one of their WSL Linuxes?

At the same time I had forgotten to write down a login password for a real PI and burnt myself a new Pi SD CArd. After it did the initial update and reboot, it came up with BCM2835 Exception stack errors and hung. I burnt it again, went through the update palaver and it did the same. A quick Google and I found a suggestion that my SD Card was failing although it had burnt without error twice and switching to a different card fixed it.

Out of curiosity, I fired up Win32DiskImager and ran a verify against it and got this error message below. If you can’t read it says Verification failed at sector 8192. Well at least I now know and the SD Card was about to be binned and I took a last look at it.  4GB! I’d been burning a 7GB image onto it; no wonder it crashed.. I’m amazed it ever burnt and worked… I’d used it in the past for the DietPI distro which is much smaller.  Most of my spare SD Cards are 8 GB; that’ll teach me for not checking…

SD Card failed verification in Win32dioskimager

 

Now this is interesting – Interactive Fiction

Now this is interesting – Interactive Fiction

Choicve of Games Interactive FictionIt’s not really a thing done in C, although you could do. I came across a site that offers commercial Interactive Fiction. As they put it “text-based, multiple-choice games. ” and “By using text, we can interact with the imagination in different ways from a graphics-based game. We can also allow game designers to quickly and inexpensively produce games in comparison with graphics-based games.”.

Their games aren’t free but aren’t that expensive either typically $5-$8 or thereabouts and you can get them on mobile as well as via Steam. A recent game they’ve just launched has 1.2 million words of text (that includes the game scripts) which is phenomenal considering that most novels are 50,000-90,000 words.  Their games use their own open source scripting language called ChoiceScript which is a very simple programming language.  You can read their online manual (over three web pages) which should take maybe 20 minutes long. You can also play some of their games partly or fully free on the web.

The games are put together through a series of scripts. There’s also a free IDE for creating the scripts from a 3rd party. If you think writing a book is hard then writing an IF (Interactive Fiction) game must be like that but on steroids.

Games like the 1.2 million words one have multiple paths through with different plot lines which aids replayability. This isn’t anything like the Fighting Fantasy books (Name drop: I worked on a football game for Ian Livingstone in the mid 90s) because ChoiceScript has variables. This means that characteristics can improve over time and you can make decisions using <. >, <=, >=,==,!= just like in C etc. It doesn’t have arrays (there seems to have been some attempt at adding them in 2015 but I suspect it didn’t work) but there are subroutines and Goto which lets you create a structure.

You can add graphics but the games I’ve looked at don’t use them much if at all. The pictures are painted with words and this company have a very wide variety of games on sales including RPG, superheroes, science fiction, mysteries, war and pirates amongst others. The game mechanics are multiple-choice questions which doesn’t sound very glamorous but its like reading a book; your mind fleshes out the backdrop. The original Colossal Cave Adventure game is an example of Interactive Fiction and not coincidentally the Awards for Interactive Fiction are called the XYZZY Awards.

As the saying goes “Everyone has a novel in them”. IF is one way to bring it out…

 

A Minecraft clone in C

A Minecraft clone in C

Open source Minecraft clone in CI’ve never really been a great fan of Minecraft, though it is a great and wildly successful game. My reason for mentioning it is that there’s a C open source version of it and it’s cross-platform as well on Windows, Mac and Linux.

It was developed seven years ago and has had the odd update since with the most recent earlier this year.

The developer (Michael Fogleman) has also provided an online server for it and has some Python in it to make that possible but 80% of the code is C. It uses OpenGl to draw the graphics; so if you want to write your own Minecraft clone, this is a good program to study. As usual I’ve added a link into the C Code Links page.

 

A different way of generating a dungeon level

A different way of generating a dungeon level

Dungeon
Image by Artie_Navarre from Pixabay

I talked previously on how I generated dungeon levels by splatting down squares and circles of random sizes and then linked them together by corridors.

But another way that might make levels look not quite so procedurally generated is to first compile a catalogue of dungeon rooms.  These are “hand drawn” in a master text file, perhaps something like this:

*****|****
*        *
=        *
*        =
*        *
*****|****

Where * marks the walls,| a possible location of a vertical running corridor and = the same for horizontal running corridor. Each entry in the catalogue has a size (vertical, horizontal) e.g. (6, 11) for the room above and the generator program has to find space for this and then see which of the four doors it can connect to.

You can decide if you want a dead end room or one with more doorways. Any unused doorways are just replaced with a * for wall. Single door rooms might have a secret door that the player has to first discover and unlock/open.

When the generator starts, it reads in the master file, builds a list of rooms and then starts using them to generate levels.

The advantage of the catalogue method is that you are free to create as many room sizes and shapes as you wish and they don’t have to be rectangular or square. Yes the level is procedurally generated but using this methods makes it look less so. You can have very small rooms (2 x 2 anyone?)  or massive hallways with lots of columns.

Time to upgrade Visual Studio?

Time to upgrade Visual Studio?

>NET 5
Visual Studio assembly versions

It’s more relevant if you are a .NET developer ie C# or VB.NET, but .NET 5.0 (including C# 9) has just been released. It was an almost 4 GB download and followed by a reboot but I’m now on .NET 5. No more .NET Core or .NET Framework unless you are developing GUI applications on Windows.

But you can also develop on Linux or Mac, but not using WinForms or WPF. Those are Windows only.  This page has downloads, or you can do what I did and upgrade through the Visual Studio Installer.

Also if you are on C (on Windows) you get C11 and C17 if you specify a compiler flag. You’ll see this on the Visual Studio 16.8 release notes.  I like to be on the (bleeding) edge of things. Maybe it’s FOMO (Fear of Missing Out). But I do find it interesting to see where the Linux support is going.

Quicksort in C

Quicksort in C

Sorting
Image by Clker-Free-Vector-Images from Pixabay

Normally I wouldn’t mention a sorting algorithm, but by a lucky coincidence, when I had my interview before going to university (Queen’s University Belfast), the bloke who interviewed me back in 1977 was the inventor of Quicksort. Tony Hoare. He left very shortly after so I never got him as a lecturer,

As algorithms go, Quicksort is one of the faster. it works by splitting the list in two and then recursively sorting the two halves. The simplest sorting method Bubble sort uses a double loop and for very small numbers of items to sort is generally more efficient than Quicksort, but it doesn’t take long for Quicksort to catch up.

I was reminded of Quicksort by seeing this article and thought it would be interesting to do a series of test of sorting 5-50 items a few thousand times each in Quicksort and Bubblesort and see where the crossover point is. How many items is the minimum for Quicksort to become faster than Bubble sort?

If you take the Swap functions and BubbleSort from here and partition and quickSort functions from here and use my high precision timing code and add the code below then you can run the comparison. I did it twice, once in Debug and once in Release and the difference is much larger in release. I’ve zipped up the source code including the Visual C++ project code (It is C source code not C++ btw) and high performance timing code and put it on GitHub.

#define NUMLOOPS 5000

int main()
{
    int startarr[]= { 86,91,50,5,79,64,2,57,26,63,12,61,12,92,40,38,11,
                 94,90,38,24,25,54,75,67,56,7,9,32,26,54,48,51,28,
                90,50,37,53,8,75,30,25,59,57,92,42,25,33,84,46 };
    int arr[50];
    stopWatch stw;
    double bTime, qTime;

    bTime = 0.0;
    qTime = 0.0;
    for (int i = 3; i < 50; i++) {
        printf("Bubble sort for %d = ", i);
        for (int j = 1; j < NUMLOOPS; j++) {
            memset(arr, 0, sizeof(arr));                     // Clear arr
            memcpy(arr, startarr, sizeof(int) * i); // copy in i elements
            startTimer(&stw);
            bubbleSort(arr, i);
            stopTimer(&stw);
            bTime += getElapsedTime(&stw);
        }
        bTime /= NUMLOOPS;
        printf("%12.10f.  ", bTime);
        printf("Quicksort sort for %d = ", i);
        for (int j = 1; j < NUMLOOPS; j++) {
            memcpy(arr, startarr, sizeof(int) * i); // copy in i elements
            startTimer(&stw);
            quickSort(arr, 0, i - 1);
            stopTimer(&stw);
            qTime += getElapsedTime(&stw);
        }
        qTime /= NUMLOOPS;
        printf("%12.10f\n", qTime);
    }
    return 0;
}

These are the first 15 and last 5 output lines in Release. The Bubble sort time for 7 is a bit of an oddity just for the actual numbers sorted. But its clear that by 11 values that Bubble sort is slower than QuickSort and the difference just gets worse up to 50. So up to 10 elements, Bubble Sort wins.

Bubble sort for 3 = 0.0000000358.  Quicksort sort for 3 = 0.0000000412
Bubble sort for 4 = 0.0000000407.  Quicksort sort for 4 = 0.0000000445
Bubble sort for 5 = 0.0000000474.  Quicksort sort for 5 = 0.0000000485
Bubble sort for 6 = 0.0000000677.  Quicksort sort for 6 = 0.0000000448
Bubble sort for 7 = 0.0000000191.  Quicksort sort for 7 = 0.0000000097
Bubble sort for 8 = 0.0000000405.  Quicksort sort for 8 = 0.0000000601
Bubble sort for 9 = 0.0000000510.  Quicksort sort for 9 = 0.0000000434
Bubble sort for 10 = 0.0000000568.  Quicksort sort for 10 = 0.0000000653
Bubble sort for 11 = 0.0000000633.  Quicksort sort for 11 = 0.0000000566
Bubble sort for 12 = 0.0000000702.  Quicksort sort for 12 = 0.0000000606
Bubble sort for 13 = 0.0000000845.  Quicksort sort for 13 = 0.0000000743
Bubble sort for 14 = 0.0000000931.  Quicksort sort for 14 = 0.0000001145
Bubble sort for 15 = 0.0000001040.  Quicksort sort for 15 = 0.0000000827
Bubble sort for 16 = 0.0000001142.  Quicksort sort for 16 = 0.0000000892
Bubble sort for 17 = 0.0000001448.  Quicksort sort for 17 = 0.0000000974
...
Bubble sort for 45 = 0.0000008478.  Quicksort sort for 45 = 0.0000002737
Bubble sort for 46 = 0.0000008561.  Quicksort sort for 46 = 0.0000002524
Bubble sort for 47 = 0.0000008948.  Quicksort sort for 47 = 0.0000002643
Bubble sort for 48 = 0.0000009421.  Quicksort sort for 48 = 0.0000003147
Bubble sort for 49 = 0.0000010042.  Quicksort sort for 49 = 0.0000002804

 

 

My other side project continues

My other side project continues

Smartphone
Image by Gerd Altmann from Pixabay

This is the social mobile multiplayer game I have mentioned before. The initial game creation program is mostly working. I say mostly because I will still be adding to it. It does however create all of the game data in about 30 seconds for a game that can hold up to 10,000 players. It then creates the output files which are read by the mobile apps. These apps have yet to be created but I’ve decided to do some early testing using non-mobile apps that I’m working on.

What I want to do is prototype the mobile app, not particularly visually but functionally. To this end I’m creating a C# client desktop app that does everything that the mobile app will do. It can read the data (directly rather than via a webserver) and let me create new orders and upload them back to the server (or in this case my development PC).

This client will be crude and not great looking but lets me test that data is coming and going correctly. I use JSON for everything, as a data transfer format as well as storing all game data at rest as opposed to a database. It lets me hold all game data in dictionaries (with a bit of tweaking it is possible to save/load lists of objects to JSON. After loading I convert them to Dictionaries using an Id field.

                var bytes = File.ReadAllText(Lib.PeopleFilename);
                var list = new List();                
                list = JsonSerializer.Deserialize<List>(bytes);
                persons = list.ToDictionary(x => x.Id);

It works and is quick. The downside of using a Dictionary is that it occupies more bytes per element than a List. How many? Well it’s never easy to figure. .NET does not really encourage discovering how things are implemented. This GitHub project by developer Ludovit Scholtz shows the memory used by various .NET Generic Collection Types (HashTable,  Dictionary, ConcurrentDictionary, HashSet, ConcurrentBag, Queue and ConcurrentQueue) with various string lengths in a TestObject which as string, an int and a DateTimeOffset.

Storing a million objects in a Dictionary <int,TestObject> with a null string occupies 48,222,240 bytes so roughly 48 bytes per entry. I believe a List is closer to 20 bytes overhead per element. So for slightly more than double the memory use, using a Dictionary gives a tangible performance yield.

,