Tag: game

And finally the domain is correctly hosted

And finally the domain is correctly hosted

Google dig tool for learncgames.comSomething had gone wrong and I couldn’t change the DNS because it was showing up in somebody else’s account. After I proved ownership of the domain by adding a TXT record they fixed it and I added the hosting name servers in.

Within a few minutes the domain was hitting the server and after I edited the A record to get rid of the parking, it correctly picked up my dummy place holder index.html.

Interestingly I could only add TXT/SPF records at 123-reg.co.uk and Google’s online dig tool (shown) couldn’t pick them up.

Hiding the VPS

I decided that I’d let the main web domain take the strain from hackers trying to access the VPS. So the web game will save orders into gamedomain.org/n/orders and get results from gamedomain.org/n/results for game n. (Not the real game domain!) But behind the scenes the VPS that runs the game processing code will pull orders from that orders folder. The VPS is located elsewhere, not even in the same hosting and after processing, it will copy the results back to the relevant results folder.

This solves a couple of problems.

  1. Getting the orders in (and results out) requires a web interface. Because the game is based on the postal model, it’s quite feasible to keep the web interface and the processing separate.
  2. Increased security. If you don’t know where the VPS is located, it’s somewhat harder to hack it!

I’m also experimenting with having a web command interface. Commands get picked up pulled to the VPS and executed. For example creating a new game, pulling in game processing logs and so on.

New domain appears to be cursed

New domain appears to be cursed

Web design
span>Image by EstudioWebDoce from Pixabay

Back about ten years ago I gave a few lectures on what it took to setup a website. I’d ask the audience (who were not technical) for ideas, showed them how to search on a registrar, find a domain name, register it and it would be live usually within 15-30 minutes.  Then I’d add some content and publish it. It worked every time.

So you’d think the process would be smooth as silk now. Think again. I registered a domain about three weeks ago. I’d had the .eu version for a game idea but of course being .eu and me not living in a EU country and so on so I decided not to renew it. However the .org of it was available and I registered that.

Last week I decided to configure hosting, buy a SSL certificate and start using it. Only when I added it to the server I rent (a reseller account), it didn’t show up on the domain page. That’s where you get the name servers from. Rather than faff around with A recs and MX records, I usually set up Name Servers to point to the hosting server. For that of course you need to know the name servers.  But the domain was not on the GoDaddy DNS page so no name server informtion. The hassle factor was ramping up a bit quickish. Very odd.

I faffed around for a couple of days and got onto GoDaddy’s technical support. It turned out, well they pointed out that (a) the registrar (123-reg.co.uk) had put parking on it. and (b) there was something a bit weird with the DNS hosting. Weirdly, on Chrome, the page is blank which is why I’d not seen it. When I looked at it on iPhone sure enough there was a parking page with adverts.

So I got name servers from GoDaddy and set them on 123. I gave it two days and looked and no change. The name servers were set but The DNS page on their site did not say that it was being managed by external name servers. You get that on other domains; this one for instance.

Onto 123 support and to be fair to them, they sorted it pretty quickly. Also I found out that 123-reg.co.uk and GoDaddy are sister companies. You learn something every day. So I looked at the site and it was the same Parking page except it now said Parked on GoDaddy instead of Parked on 123-reg.co.uk. Me I find this a bit cheeky; I’m paying for registering a domain and hosting but those companies are making a little (maybe more than a little) by parking my domain. I can understand that they’ve probably got a lot of domains in limbo; alone and unhosted so why not park em. But it seems a bit cheeky just doing it without saying anything.

How long to Wait?

So back to GoDaddy support which isn’t always easy. You can do it online by a chat Window and at one point I got this gem. 371 Minutes! Normally its 4 or 5 minutes… I closed the browser window and came back 30 minutes later.

Slight Delay

So the oddity I mentioned earlier; somehow, the domain was added for hosting against somebody else’s account. I can’t explain it, they can’t explain it. So they asked me to verify ownership by reverting the name servers to 123’s and adding a TXT record in the DNS with a certain name and value. Once they can see that I really do own it, they’ll change ownership of its hosting (not even the domain just the hosting!) to me.

So what used to take me 15-30 minutes is about nine days elapsed time. And it’s not finished yet. I really do think this domain is cursed.

 

A new beginning – C#

A new beginning – C#

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

You may have noticed, I stopped posting here a while back because basically I ran out of ideas for C related games stuff plus I was doing other stuff that wasn’t relevant (Delphi, C#).

I will get back to finishing my 2nd eBook, but for now, I’m working on a web/mobile game that isn’t really C related. However I thought I’d cover development of it here. as (a) part of it is C# (so technically it’s C and (b) it’s a game.  The backend part is C#, the front end Flutter which is Dart. I’m also going back to my old game design roots as the game architecture is ‘Postal Game’.

What is ‘Postal Game’ architecture?

Well first a little potted history.

Back before the web existed (late 80s), people used to play games by post. They’d fill in a postcard with the orders, post it to the game provider who would enter the details in a computer, process the game then print the results out and post that back. I discovered this (in the UK) back in 1986 and created a postal game: Casus Belli. A 16 player version of Empire. It ran for a year or so and I even attended a postal games conference with a stall back at the start of 1987 in London. By chance, the stall next to mine was run by KJC Games and we got chatting. Kevin J Cropper (The KJC of KJC Games) said “If I ever wanted to sell the game contact me” and at the end of 1987 I did and found myself working for KJC Games as a game designer and programmer in Cleveleys (just North of Blackpool). It was a small cottage industry in the UK and KJC Games was the biggest fish in this admittedly small pond.

Over the next two and a half years I created the 100 player version of Casus called Warlord. Long after I left they ran games across the web and though they don’t run it any more you can still see the page for it. My big creation was a 1,000 player postal game called Quest which KJC still runs. Not bad for a game created in 1989! They still have my name in the Advanced rules which is nice as I last did some work on it in 1996. It took me a year and 40,000 lines of code (Turbo Pascal 5) to create. The others listed helped test it, made suggestions and Steven Fairbrother who came along after I left, fixed my bugs.  When you write 40,000 lines of code in a year, there are a few bugs included…

So each turn gets posted in, processed, printed and posted back. The architecture is the same except its all now sent across the web. There’s a front end programmed in Flutter. This makes it possible to use the same code for Web and mobile development. The game is processed at a fixed rate. In this case I’m going for every three hours but slower or faster is possible. When it’s time, all orders are processed and the results made available to be fetched from the web or mobile device.

Pros and Cons

A Game Processing Engine (GPE) is a completely different kettle of fish from your traditional multi player game server. Hardware requirements are much less and you don’t manage multiple players all at once. Basically the game web/mobile uploads a zip file from each player to send in the orders and then fetches one containing the results. The GPE maintains game state, processes all orders, outputs all results and backs up the game, both before processing each turn (in case it goes wrong so it can be fixed and rerun) and afterwards,

So I’ll be posting here about my progress, programming issues and so on. If you have any queries, please don’t add them as comments – I delete any non-technical comments so instead use the contact form via the About me link on the top.

 

 

A fabulous collection of game icons

A fabulous collection of game icons

Building icons
Building icons from game-icons.net

These are from game-icons.net and are 512 x 512 pixels two colour icons and completely free to use so long as you give credit and include a copy of the licence or a link to it. There are over 4,000 but you can download a subset if you don’t want the lot. The download file zip file size for all of them as .png is only 33 MB.

Subsets include Animal (182),  Building & Place (181), Weapon (172), Symbol & Emblem (171),  Body (158), Arrow & Spear (146), GUI (140), Spike, Slash & Crack (138), Head & Face (126), Blade, Sword & Knife (126), Food (125), Liquid (122)Creature & Monster (119), Tool (117) and Board & Card (116).

What’s nice is you can have them as .svg (vector) or .png and have black on white or white on black.

They may look small when shown here but each is 512 x 512 pixels which is a lot.

 

DungeonRush – open source C + SDL 2 game

DungeonRush – open source C + SDL 2 game

DungeonRush gameI continue my quest, looking around for open source games in C that use SDL 2. The latest one is DungeonRush by developer Rapiz1 (does no one ever use their real names these days?) who hails from Wuhan.

It is not a rogue-like, but more a Snake-like game.   You move your hero around the playing area avoiding monsters and things fired at you while picking up stuff and people to give you extra lives.  As well as the difficulty levels it includes multiplayer mode as well though I haven’t tried that.

If you are learning C or games programming, it’s worth studying to see how others do things; this includes its own text drawing and high scoring and saving high scores.  It’s quite fun to play though I am rubbish playing it, even at the normal level. I’ve added this to the C Code links page. (Accessed on the top menu),

Snake games are interesting because if you use a circular buffer, no matter how big the snake grows, you can move it by just moving the head and tail elements. An O(1) operation!

Pacman in C99 that runs in the browser

Pacman in C99 that runs in the browser

PacmanSuch a simple game yet still incredibly popular. Pacman is now 40 years old. I must confess, its not one that I was great at (that would be Battle Zone- the 3D wireframe tanks on the moon game- I could play that for an hour for just 10p),

This cross-platform implementation by German programmer Andre Weissflog (aka Flooh) looks spot on and plays it as well. Most of the game has been implemented in one C source file and its definitely worth reading the comments at the top of that file to see how things are done.

For instance “audio output works through an actual Namco WSG emulator which generates sound samples for 3 hardware voices from a 20-bit frequency counter,  4-bit volume and 3-bit wave type (for 8 wavetables made of 32 sample values each stored in a ROM dump)”.

The game uses the author’s Sokol libraries which are Simple STB-style cross-platform libraries for C and C++, written in C. It’s the first time I'[d come across these and they’re worthy of a separate look at. STB is a another library that I’ll also look at in the future. I’ve added a link to this Pacman to the C Code library. Happy Christmas!

PS. Also included in the GitHub download is The Pac-man dossier by Jamey Pittman, which is a 48 page PDF and tells you everything about playing Pacman. Stuff like the logic of gameplay, mechanics, easter eggs and the hardware specs of the original game boards not to mention the infamous level 255 overflow.

Interestingly the original hardware was a 3MHZ Z80A with 2 KB of RAM and 16KB of ROM on a 224 x 288 16 colour screen .

 

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.

,

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.

I’ve started on the C++ Windows eBook

I’ve started on the C++ Windows eBook

C++ Code listing photoI made the mistake of starting by trying to convert the final version of Asteroid; all 2,200 lines of C into C++.

It got very messy because I was trying to have all the moving objects (Player ship, asteroids, bullets, aliens ship) all based on a common ancestor class but then was trying to manipulate those instances of the ancestor class and downcast back from the ancestor instances and I don’t think you can in C++. Compiler errors galore!

It was the wrong approach and I wasn’t using virtual functions. So instead I’m doing it step by step, adding on new features. Much like the original C development in 13 different steps.

Here’s the slightly shorter asteroids.cpp:

// Asteroids C++ 2020 Chapter 27
#include "game.h"

int main(int argc,char * args[])
{
	Game g;
	g.InitSetup();
	g.GameLoop();
	g.FinishOff();
    return 0;
} 

There are other classes used from Game. I haven’t put everything in one “God” class!

How pixel perfect collision detection works

How pixel perfect collision detection works

Asteroid and player ship about to collide near an explosionsThere are four types of moving object in the games. Asteroids in four sizes from 35×35, 70×70, 140×140 and 280×280 pixels, the player’s ship (fits in 64 x 64 pixels), alien ships (also in 64 x 64)  and a bullet which is solid 3×3 pixels with the four corners empty.

As these move around the screen at 60 frames per second, they will come into contact and the collision detection has to figure out when they hit or miss.

To make it more complicated, the asteroids and player’s ship come in 24 rotations, each by 15 degrees so detection has to take that into account. The image shows an asteroid just about to hit the player’s ship.. At the bottom you can see where another asteroid has been blown up (the 50 is the score) and there’s a bullet to the right.

The detection occurs in several stages. First each object has a bounding square.This shows the bounding boxes for the player's ship and an asteroid This is an invisible square that just fits round each object. For the player’s ship it’s 64 x 64 and it corresponds to the sizes of each asteroid. As the image shows, the two bounding boxes overlap and it’s in this overlap rectangle that we have to check for a possible collision.

A large asteroid and the cells it overlapsFirst things first. Every frame all objects are moved and we have to detect if there’s a chance of a collision. This is done by dividing the entire playing area (set in the book to 1024 x 768) into 64 x 64 pixels cells.  I chose that as a convenient size. Once the object’s new x,y location has been calculated, I determine which cells it overlaps.

The player’s ship can fit completely into one cell but most times it is either overlapping two or four cells. Even a 3 x 3 bullet will occasionally overlap two or four cells but most of the time it will fit completely in one.  The largest Asteroid (280 x 280) always overlaps five or six cells in each direction so it gets added into to 25,30 or 36 cells each frame.  The image shows the grid of 64 x 64 cells and the asteroid overlaps 25 cells, the player’s ship just overlaps 4 cells. The number is a count of the number of overlapping item in each cell.   It’s the bounding box size that determines if it overlaps which is why the top left and bottom right empty cells of the asteroid still show 1.

In each cell, I keep a list of objects that overlap that cell. The first step is scanning all cells with overlapping objects in to see if there are two or more objects overlapping in that cell. The SDL library includes a function SDL_IntersectRect()  You pass in the two bounding rectangles of overlapping objects and it returns a rectangle of the intersection, which corresponds to the overlap in the image above. If there are multiple objects in a cell e.g. four then you have to check to see if any of those (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) overlap.

A text file representation of a maskThe next bit is what makes it pixel perfect. For every object and its rotations, I have created a mask. It’s a file of bytes with a 1 value corresponding to every coloured pixel in the object and 0 to empty pixels.

I have stretched this image to make it the same aspect ratio and it shows 0s and 1s in the player’s ship mask.

The collision detection algorithm processes the intersection rectangle and calculates where each pixel in that rectangle falls in the corresponding mask (adjusting for rotations) of each object.  If either mask pixel is 0 then no collision occurs but if both are 1 then bam!

Although I could have used a mask of bits, it was quicker to use bytes.

During development I wanted to verify that it was pixel perfect, so I disabled explosions and instead when two pixels overlapped, I output a green pixel to highlight it. The image shows the player’s ship passing through an asteroid!Showing pixel perfect touching