Wednesday, 10 December 2014

Programming - Elite Dangerous Tools in C++

Update: You can now support this project at Patreon!

I'm back at work, and very busy, we're still waiting on the house sale... But I've got my man lab, roughly, back together... In fact I've got a little improvement in things as I'm back to a single room with shelves all around me so I can turn from my work, pick a DVD and play it whilst working!

This has meant I've re-watched the entire Black Adder series, I'm in the middle of re-watching the Lord of the Rings (yes the special extended edition)...

But, what am I working on?... Well, I'm still playing WarThunder, but I'm also playing Elite Dangerous, and I've some history with Elite, specifically Frontier.  Where I ran "The Frontier Club" later the "Frontier Users Group"... More about which later...

But as part of the club I created a series of tools, in Pascal, on which you could record your kills and document the systems visited, this was really clunky basically you noted things down on paper then when you'd finished playing the game you rebooted your Atari ST to run the tools and insert your new found knowledge, building something which I later would call a data-sheet... A database come spreadsheet hybrid... But which you could print out from the computer, making everything look dead neat and tidy.

I used to have reels of system information and lists of kills counted on the wall, and each weekend after playing Frontier, I'd laboriously enter new information, run a tally and print out my new score sheet and pin it on the wall... I don't know if any of the club members did this, but I remember getting some feedback from users asking for the source code... Though what seemed to amaze most club members was my using "Timeworks" desktop publisher on my ST and from my Citizen 120D+ 9 pin printer producing real looking news paper style news letters... I think I actually got more requests as to how I did that, and proof with photographs requested, than I did requests for save games!

Anyway, I remember that, let me know if any readers were club members!  And I remember the frustration of noting stuff down and then manually updating the system... This was a limitation of the Atari ST, which apart from limited TSR programs you could only run one application at a time, there was no process switching at all.

Of course on a current PC with Elite Dangerous running we can process switch, we run tens, dozens, even perhaps hundreds of processes at the same time as you're playing the game on your machine.

And we could just hook into the memory of Elite Dangerous from another process and scrap out any information we wanted, however, Frontier Development have kindly asked that tools not do this, and that they not wiretap the network connection and capture information from it... Because doing either of these techniques one could, and some tools did, scrap out the market data to optimise peoples play experience; just like us old veterans did with the original titles "back in the day".

So, if we can't scrap the memory or intercept the transmissions, and of course paper and pen are too labourious, what can we do?  Well, I started out with Open Office, a spreadsheet open and created a tab for each system I visited I noted down for each station the basic goods available and I have dozens of tabs, hundreds of trade goods, multiple resource extraction sites listed and even combat sites listed... And it's totally inaccessible, it's very hard to update, slowly goes out of date as the markets change in game, and it take almost as much time to generate this information by hand as spend playing the actual game.

It does give me a great advantage, and starting a new game I soon jumped from 1000 credits to three new ships and a healthy 500,000 credit bank balance... Is this useful... Most certainly... Is it worth the effort?... No.

I'm a busy married man, I work all day, I try to enjoy the limited game time I get and updating a massive amount of spreadsheets seems a bit more like hard work than entertainment... If you think that spreadsheets might be entertaining, you have my sincere condolences.

Locked out of accessing the client or it's network traffic is there some way in which we could scrap data out of the game then?  Well, we humans do it all the time, we simply read the screen... So I set about writing a first tool, this was a very simple took which hooked into the windows keyboard controller at the lowest level and if I pressed a key (even in a fullscreen client) the tool took a screenshot and stored it...

I could therefore capture the market data as I landed at each station.  But I again had to manually enter the station and system name... Adding these images to a series of tabs I had a quick way to switch between images of market data... But it had some problems... One problem (and a persisting problem) is that on my screen at 1680x1440 I could only see part of the market listing at a time, I had to scroll down and take two images to see the whole market, meaning I needed to write another toll to stitch the two images together to get a complete listing... This image stitching became the focus of another of my forays into Image Processing.

Next I wanted to uniformly present the text, so I used previous code I'd created (in C#) ported it to C++ and so was able to drop out the colours... In grey scale the markets looked nicer.

This then lead to a mini-eureka moment... With this complete market list, in black and white, could we apply the Sobel operation I've previously looked at and so give ma a crisp edge to the text?

Yes, I could... And did... So now, could I find an OCR tool to read this text for me?

And this is where I wanted to start to talk about the development, the image processing to this point has been plenty of code to get to grips with, both the windows code to gather the raw image, then stitch and prepare the greyscale copy and apply the sobel mask is a big piece of code and making it pretty code was also a challenge as you're always trying to get things done rather than make code others, or even yourself, can come back to later.

But OCR... Making code to read the text... That's a huge new challenge for me, something I've touched on before, but never put my teeth into.

You can buy off the shelf OCR solutions, but I don't, and I'm pretty sure not many of my readers have the $6000 odd dollars they're asking for.  We could use the Microsoft Office OCR plug-ins... But, this locks us to Microsoft, locks us to using Office and paying a license fee... I don't own office and don't want to make a tool, which others might use, that is subject to such a fee.

What options for OCR are there for me?.. Well I could look up and implement some Neural Network stuff... but I've never really understood Neural Networks, those that know about them tend to spout al-sorts of mathematics and never present code, where code is presented it either doesn't work or is uncompilable.

What have the open source community got?... Well there is a series of tools around, one called Tesseract caught my eye, but it was wholly unwieldy, it didn't want to compile on windows and it relied on several dozen other libraries each of which had to be pre-built or would not build on windows.

Hows about simpler OCR?... Pattern matching?... Yes, these are options as well...

This series of developments will form the focus of my next few programming posts, so stick around.


  1. suggest you look into Hidden Markov, or support vector machines. Im using with Accord.NET - Support Vector Machines and its pretty darn good...

    im doing exactly the same thing with ED.


    1. I integrated Tesseract OCR in the end, spin forward to February 2015 in the blog for examples.