Game Design, Programming and running a one-man games business…

Pak files

I just added pak file support to Democracy 4. Its something I already had coded for an earlier game, but I had to do a bit of fussing to get it to work properly with democracy 4.

Pak files are basically big phat files that contain other files inside them. If you are a new developer, you probably have no idea they exist, because you probably use unity and AFAIK they handle it for you. Pak files are pretty old school, as I recall Doom and Quake used them (maybe called wad files), anyway the principle is pretty simple:

A pak file contains two sections, an index that tells you where all the other files are inside the main section, and a big phat list of data that is the contents of those files. All of this gets stuck in a single flat blob of binary data. A class exists that lets you grab the memory address of the file you want if you pass in the name of the file, so hopefully to anybody who didnt write the pak file code, using it is easy. You can read the contents of a file just like its on disk, except you have to use functions that read from memory, not ones that explicitly read disk files.

In my case, that meant stepping into the engine code, and the opengl stuff that reads in graphics files (we are only using a pak right now for dds and pngs), and just changing the contents of one function. Instead of using this code to load the data ready for creating a dds:

fread(filedata, filesize, 1, fp);

I now use this

memcpy(filedata, GetPakFiles()->GetData(pentry->StartOffset), filesize);

Big deal :D. Similar changes happen for pngs. To keep things super-simple, the old code for loading a file will run if the pak file reports it cant find that file, so you can stick a ‘loose’ png or dds file into the bitmaps folder structure and it will still get found, and the rest of the code doesnt even know the difference, which is perfect for mod support.

Most of the hassle in getting this to work was just writing some code to enumerate (that means list really…) the contents of a folder from within the pak file. I had to support this for stuff like minister profile pics, because the code previously would ask ‘what files are in this folder, I need to know so I can select a random one’, and now that code gets handled by the index at the start of the pak file instead.

So why bother with this?

Basically speed. Do you know wwhat the read speed of your hard drive is? Checking a random new one on shows 6Gb/s. I assume thats bits not bytes, so thats 750MB per second. My hard drive is a little bit older, so lets say 500MB/second. I’ll just copy a big chunk of the source and obj files from D3 to another disk, brb…

Ok…copy speed is between a low of 1MB/second up to 100 MB/second. Wow. Thats so much slower. Why?

A HUGE amount of bullshit happens on a PC when you access files. To simplify it, it goes something like this: *deep breath* You ask to read in a file. The OS then looks up the file table to check that file exists. it then asks the security system if the current user has permission to access that file. When it gets a yes, it then opens that file, and sets attrributes so other processes will know that file is in use. The antivirus software then kicks in, and hooks into the file read so that it can check to see if that file is excluded from scans or not, and gets ready to analyze its contents. The O/S then has to use the file-table to work out where all the various scattered chunks of that file are, and start reading in each block. This means talking to the driver, and ultimately to the hardware, which may also have to check its cache to see what blocks have been cached and whether or not it has to start the glacial process of spinning an old physical drive or not (faster with SSDs obviously).

THEN! when the file read is complete, we can close that file again, notify the system that its not in use by our process any more. We can then start the process of opening the next file in our list..

You do that bullshit for EVERY DARNED FILE. But the good news is… if you have a big phat pak file…you do it once. Just once. The rest is free.

So Democracy 4 goes the extra mile, because our pak file is small (only a few hundred MB). We dont just open the file on startup, we stream all 200MB into RAM. That should take way under a second. We then have the entire file system of dds and png files in memory already, and able to be loaded almost-instantly into our engine. (RAM->VRAM is mega fast)

99% of players will not notice the speed difference. But if you have especially shit anti-virus running on a laptop, in low-battery mode, with an extremely fragmented hard drive, running democracy 4 on a train, you will be glad I bothered. Its really easy to code. My PakFile code has 263 lines in it. Many of them are whitespace or comments.

The death of UK Television

Its shocking just how awful UK TV is these days. I am old enoguh to remember just 3 channels, broadcasting stopping totally around midnight, and no TV at all during the mornings. TV quality was actually BETTER back then.

We now have about 500 channels, and they are uniformly awful. As a creature of habit aged 50, I still optimistically find myself seeing if anything is on TV tonight. There never is. its all dumbed-down, patronizing crap, repeats, movies from fifteen years ago, or TV that honestly seems designed to rot peoples brains. The cheaper the TV the better, so reality TV is king, and anything where the ‘content’ is just random unpaid members of the public waffling is ideal.

British Tv just cannot compete with amazon prime, the disney channel and netflix. its game over. Why the hell would I watch UK TV when programs that are world reknown, cost 100x the average BBC budget and everybody is talking about…are just a convenient click away, scheduled whenever I like them? Of course I am FORCED to pay for thew BBC by law.

For a LONG time I listened to radio 4’s political ‘today’ program when I woke up. I now wake up listening to the theme music from Star Trek:DS9. I feel I’m more informed, educated, and calm listening to that than I do to the shouty, interrupt-laden cross-talking imbecilic bullshit that passes for ‘political journalism’ in the UK these days.

Just a FEW of the traits of modern TV ‘news’ (ha!) reporting that drives me insane.

  • TWO presenters. One male, one female because….why? wwe need to double the salary? I dont give a fuck what gender reasds the news, I only need one person to do it for fucks sake.
  • SMILE SMILE SMILE. Why? You just look like a drooling imbecile. I dont smile that much in a month.
  • BIG HAND GESTURES. For no apparent reason. Why the hell? just talk. I dont need you to immitate someone doing fucking origami as you explain the news.
  • TOTALLY random EMPHASIS on the OCCASIONAL word for no EASILY understandable REASON. Nobody on the planet talks like this to their friends. What on earth are you doing?
  • Completely pointless cross-focus camera effects to go from a leaf, to a street sign as somebody is talking. WHY?
  • Taking some random item in a scene and using it as the background for a CGI graph to show that you can do that. Am I supposed to clap?
  • Walking slowly towards the camera as it pans backwards to reveal that you are in a busy street, walking extra slowly, and babbling to nobody like some sort of maniac. For fucks sake stand still.
  • Cutting back and forth between the person being interviewed and the interviewer, nodding, like an idiot when we all know these ‘nodding’ bits are filmed afterwards. We understand the concept that you are still there for fucks sake.

All of this gives me the strong impression that modern UK TV is aimed at idiots. Anybody who can tie there shoelaces is supposed to get their news online, but hopefully not from the similarly dumbed-down drivel and lecturing, patronizing bullshit that is the BBC news website.

Oh By the way… climate change is in full force right now. its 45 degrees above normal in the fucking ARCTIC CIRCLE and nobody has even put this on the bloody news. You have zero future, and we are heading towards a climate armageddon. Enjoy Love Island.

That's a lot of singletons! 150,000 people apply for the new ...

Democracy 4: The fixed income rewrite

About a week ago I had this mad idea that it would be cool to plot every single Democracy 4 voter’s wealth on a graph, so that you could see where they were clustering in a nice easy-to-understand way. Within an hour it worked, within a day, a new rewritten version that looked much nicer with blue dots on it was done, and I was tweeting, and people were saying ‘yay’, and then everything went fucking mad.

By hovering the mouse over one of those blue dots, you could see a breakdown of how that persons income was affected by every government policy or situation, indirectly, through their membership of voter groups. This data already existed in Democracy 3, it was easy, it was just GUI code, and done really quick… and that showed me what an absolute mess the simulation was…

Democracy 1,2,3 and 4 are all coded as a homebrew neural network. Every neuron has a value either capped from 0->1 or -1->+1. Everything in the game is a neuron, a voter, a voter group, a policy, a minister, an event…everything. Voters also have an ‘income’ neuron which tracks how much money those voters have. So…in supersimplistic terms if you want to know why Bobs income is 0.78, you look at the 21 weighted inputs from all the voter group income neurons, and you see all the +0.2, -0.1,+0.32 etc, that adds up to 0.78. If you want to go one stage further up the hierarchy you can track the origins of those effects to policies etc.

Thats worked for 3 games perfectly. But its a crap system.

The trouble is, peoples incomes are on a 0->1 scale. And all effects are percentages. So for example if free bus passess give retired people a 0.05 income boost, that increases the income of all retired people by 5%. Fine?


Because working class ex-street sweeper mavis just got a bus pass worth $500, but retired hedge fund manager Boris just got a buss pass worth $15,000. WTF? why does democracy hate poor people? The problem is that we have only ever been able to use effects to apply percentages to incomes. That means EVERY benefit, or tax, or effect is proprotional to your income. That means lambourghini drivers pay more in car tax than skoda owners (maybe intentional), but means the state pension depends on how wealthy you already are.

Its fixed. it was hard.

Basically I have had to code an entirely new shadow system of fixed-income neurons that can cope with values beyond 1, and then (this is the hard bit) written code that stitches it all back together internally so that we can still use the same mechansims to move people between middle-income and poor etc, and still display everything in the UI as though nothing has changed.

This was hellish, because it also means restitching together lists of totally different UI items on the fly with different calculation methods to come up with a result that looks the same as it used to. Its taken a week of fixing edge cases, checking, altering UI, and writing lots and lots and lots of code which mostly will go underappreciated :D.

But thankfully it now works, and it means we can have effects in the game which are +10% income of retired people, and also effects that are +$10,000 income of retired people, as the designer or modder sees fit. This means helicopter money can actually be fixed for everyone, free school meals no longer serve foie gras to rich students, and so-on.

You wont notice it immediately but its a massive improvement in the underlying simulation code in the game. It stressed me and tired me out so much to do it that im forcing myself not to code today so I can recover.

Tesla in 2020. A good investment?

Tesla’s stock price recently surged past $1,000. it has since fallen back slightly, but I have no doubt it shall return. The last few months have been a roller-coaster for TSLA stock holders. What can it all mean? Is the company now over-valued? or is this actually the market catching up with reality?

It always helps to get some of the big important numbers out there as a basis to analyze this sort of thing. Numbers are always open to interpretation, but you still need them…

  • In 2019 Tesla produced 367,500 vehicles. source.
  • The market cap of the company is currently $180 Billion.
  • The automotive gross margin is approximately 25%
  • YoY revenue growth is 38%

If you look at the number of cars Tesla makes, its still quite a niche player. How can it possibly be worth the same as Toyota, or SEVEN times the value of the ford motor company? How is this a sensible valuation? Here are some points to consider:

  • Tesla has decent profits on each car

Teslas automotive margin is actually REALLY good, and its been growing too, from 20% a year ago to 25% today. How? Tesla do NOT advertise. That already saves them a fortune. They do NOT have any middlemen (you buy online, or in store, direct from tesla), which cuts out a whole other bunch of middlemen, and tesla have such a reputation for good tech that they can attract top talent without exorbitant salaries. The promise of tesla stock is worth way more to potential senior hires than any actual cash anyway.

FWIW Fords gross margin has varied between 18% and 12% over the last 14 years. Thats ford, one of the biggest car companies on earth, and one of the oldest, yet they aren’t as good at making profit from a car as Tesla…

Plus Tesla is the first car company to make actual money from software. The FSD (full-self-driving) hardware is in every new car (they don’t use LIDAR so its cheap), and you can upgrade your car after purchase to enable FSD or in some cases extra range or speed. Thats pure 100% profit.

A culture of constant re-investment and expansion has meant the company has not posted a full year profit yet (although it has multiple sequential quarters of profitability. Expect that to change very very soon, probably in a few months.

  • Tesla is growing like crazy

The global car market is in real trouble, but the company that is not only bucking the trend, but seemingly accelerating into space (literally) is tesla. check out production:

Tesla Has Best Ever 1st Quarter — 102,672 Vehicles Produced ...

Note that Tesla is selling every car it makes. These are not cars made to sit on showroom forecourts for months hoping someone walks buy. There are waiting lists for these cars all around the world.

Remember that its PROFIT in the FUTURE that determines a company valuation. Ford has historically made a LOT of relatively unprofitable ICE (internal combustion engine) cars that suddenly people don’t want. Thats not a good thing, but more of a liability. Hundreds of thousands of employees, and many factories designed and trained to make internal combustion engines are basically a stranded asset (worthless). Ford is well placed to continue breeding horses just as the automobile has been invented.

Note also that the VAST majority of Tesla’s output is from a single car factory in Freemont, an ex GM/Toyota plant for ICE vehicles. Their first dedicated EV-factory in china is currently ramping up, so expect rapid growth. Plus they are already building the 3rd car factory in Berlin. A fourth is expected soon in Texas. This company has only just got started…

  • Tesla has zero competition

Fancy an electric Audi? you can get $20,000 knocked off the price of a new e-tron, a supposed tesla-killer that nobody wants. Or maybe you want a jaguar I-pace? Both of these cars were heralded as the cars that would crush Tesla. Both are relative flops.

Tesla = 75–85% of US Electric Vehicle Sales | CleanTechnica

Even the chevy bolt, a car that GM LOSES money on, is no real competition.

  • Tesla dominates in a rapidly growing market segment

Electric cars are the exception to the declining car industry, in terms of growth. Would you want to be the biog player in this rapidly accelerating market, or a dinosaur left at the top of the crumbling mess that is ICE cars? Dieselgate was the first shot-across-the-bows, but the experience of clean air post-covid19 and the looming nightmare of climate change shows that there is no future for ICE cars. Countries all over the world have already set dates to phase out ICE sales. The leading car companies of the future will be electric car companies first and foremost.

Demand for battery metals strong despite weak EV sales

People always talk about Tesla as a car company, but thats similar to how amazon was a bookstore. Tesla is a transportation and energy/software company, whose current main seller is a car. Tesla energy is a little-appreciated but growing part of the business, and Teslas solar roof tile product is finally starting to ramp up. This diversifies the company and enable it to adjust to changes in demand across sectors.

Software is the real wild card, which brings us on to the topic of….

  • Autonomy

People do not generally appreciate the HUGE lead Tesla has over every other company on earth in the field of self driving vehicles. Everybody else is using HD maps and local geo-fencing, or LIDAR, neither of which actually scale. You can get a REALLY cool reliable little bubble car that drives around a geo-fenced and controlled environment like a retirement village right now, that will work amazingly 99% of the time. Lots of different companies are showing off stuff like this, as a way to grab the cash of ill-informed venture capitalists. Tesla is not trying to hype up specific cases, its driving to solve autonomy in the general case, globally, in all conditions, 100% of the time, and its getting there.

The difference between 99% autonomy and 100% doesn’t sound like much but its game changing. 100% means no steering wheel, no driver, and no worries. 99% means basically very good cruise control. My Autopilot v1 Tesla model S is VERY good on highways. It makes long drives safer and way more relaxing. Its not autonomous. When its 100%, I can get drunk in restaurants. I can watch a movie or read a book on long drives.

Tesla Self-Driving Demonstration | Tesla UK

A 2 hour commute is currently a nightmare, but when you can literally be asleep, or reading, or playing games, or working in the car… its not so bad. This will change where people live and work, and how they work. Imagine Uber, but safer, with nobody you have to talk to (or tip), and at one quarter the cost. You could play games/stream music/watch TV in your uber, because nobody else is in it…and thats safer for lone women too. Autonomy at 100% is a MASSIVE big deal financially, and there is a VERY good chance Tesla gets there before anybody else and just eats Ubers breakfast, lunch and all other meals.


Waymos autonomous cars have driven 20 million miles. In one country. AFAIK in one state. Woohoo. Tesla were at 2 BILLION in November last year. Don’t forget every car they make has autopilot hardware, and the rate they make them is accelerating. Some people value waymo at 30 billion. Lolz.

So to sum up, I don’t KNOW the true value of Tesla, because the companies output and capabilities are accelerating so fast its hard to pin a target on it. Given the actual number of cars shipped, it seems a high valuation, but that number will change a lot in the next year or so, and as EV incentives come out of the EU and the UK, and ICE sales continue to plummet, I can see more people seeing a $1,000 TSLA stock price as actually quite a bargain.

BTW the companies stock has tripled since the last time I blogged about why its a good buy.