Author: Josh

  • Enhancing my pet game project with CastleDB

    Using static databases and simple code generation to describe almost-dynamic game content.

    I’m a solo indie dev working on my magnum opus of undisputed genius, _space_train. The daily grind of handling all aspects of game development is my burden, except when I’m not doing that because I have a regular job as a software developer instead. The major flaw with _space_train is that there’s no compelling reason to play it, and that that will continue to be the case for the foreseeable future. Nonetheless, if you want to follow development and watch as I slowly descend into madness and ignominy, it has an itch.io page here with a downloadable build and a Twitter here.

    I had an issue building _space_train, my magnum opus of undisputed genius. The path to complex user interactions lay in the direction of adding ‘content’ for prospective users to engage with. In material terms, this meant adding more of the things that people do in the game: items to pick up and use. The problem was, adding new items was proving a surprisingly laborious task for me, the developer. Items in _space_train are represented by unique id numbers associated with an entry into a centrally managed map which has an id, a type, and a ‘display modifier’, a bitfield which specifies the appearance. The list of possible item types is represented with an enum, and a separate mapping exists to convert between the enum value for each type name and the string name for each type — the string value being necessary for accessing external resources for each item type, specifically Lua scripts which say what they do in various scenarios.

    Adding a new item then, means adding both to the enum, and then to the name map, the name of the new item. In addition, over on the client side, the item type then needs to be associated with coordinates into the item texture, represented in another enum. All this before any actual material properties of the new item can be expressed!

    A player standing by a handful of items in _space_train.

    The elephant in the room is the possibility of loading and representing item types at runtime. This would necessitate separating items into some extra-compilatary zone where they can be manipulated distinct from the game code, all together all in one place. This was appealing in some ways — it would remove business data from the scrappy C++ engine I’ve insisted on building and relocate it, in a potentially more manageable format. I didn’t want to give up, however, on the ease of referring to item types directly in code, or take on the overheads of runtime look-up.


    Around a year ago, I became aware of a software development tool called CastleDB (www.castledb.org), which appeared to do something to do with static data. The website described it thus: “CastleDB is a structured static database easing collaboration.”

    Structure sounds good. Static sounds good. Unfortunately there’s only me working on _space_train so the collaboration might go a little under-utilised, but I was still excited.

    Unfortunately, at the bottom of the page, castledb.org also says this: “CastleDB was created using the Haxe technology, it also allows some Haxe-specific integration.” I’ve heard of Haxe, and the website for it only really confused me more as to what it does, I’m aware it’s some sort of web-development-y thing. And I myself, a terrible C++ programmer, don’t understand the first thing about it — or any other webdev library. Thrown into confusion like this, unclear over whether or not CastleDB was something only web developers got to use, I did what any good programmer ought to do: I left the tab open for a full year and skim-read the page again, once every other month.

    Eventually, I decided to go one step further: I downloaded the damn thing. Inside the zip there was an index.html, which loaded into a blank spreadsheet with buttons that didn’t work and an error in the console saying “CONSOLE ERROR HERE”. It turns out CastleDB is bundled as some kind of self-hosting web executable, and I was supposed to go against years of instinct and open cdb.exe rather than the enticingly-named index.html. Never grow complacent. With CastleDB opened properly, I found that it was actually exactly what I wanted: for want of a simpler phase, it’s Excel but not dynamic. It’s phpmyadmin but not php or mysql. It’s hand-editing JSON but without any hand-editing JSON. You enter static data in a familiar-ish interface (minor gripe — copy and paste doesn’t work on MacOS, but I’m narcissistic enough to enjoy retyping my own work) and it comes out in JSON. This was the start of something good.


    What I wanted, was to enter data using this convenient interface, and have it represented in code for me to use when designing game elements (in theory the bulk of game development; in practise I merely add more engine features and release nothing). This necessitated some kind of transformation of the data into code, either at compile time or at runtime. As discussed earlier, I’m loath to defer this process to runtime for several reasons, most of which boil down to wanting to pretend my program is less complicated than it is. If elements are loaded at runtime, then all references discovered while debugging are contingent on that load, and it’s harder to have good instincts about what is out of place or unusual. So I wanted this to happen at compile time.

    The missing link here for C++ development specifically, is some kind of all-constexpr JSON processing library that can automatically infer data structures from some input. I am not the person to write that library though, and decided to make do with some homegrown codegen.

    Codegen is the art of code which produces code, and often leads to some of the worst abominations of programming imaginable. All the personal terribleness of the programmer who wrote it, combined with a computer’s indefinite ability to spew vast reams of procedural content. Nonetheless, codegen has a proud history in games: in the Wolfenstein 3D Black Book, Fabien Sanglard describes how iD used codegen to produce (and patch!) code for transformation tables for sprites at distance at runtime, in order to minimise the size of the executable shipped. If it’s good enough for shooting Nazis, it’s good enough for me.

    JSON was originally invented to serve as a serialisation format for Javascript, so I immediately decided the best tool for this job would be Python. Python has a first-class JSON loading library, and very simple string manipulation functions (compared to working directly in C++ for this). Using a Python script to write some simple C++ types and functions into a header and cpp couldn’t have been simpler, and I very quickly had a working enum class generated, included and compiled.

    My simple database of space-tomatoes.

    From this quickly thrown-together table of existing content, a handful of Python lines in the fashion of the ones below:

    output_h.write("""
    enum class list {
    """)
    for item in item_db_items:
    output_h.write(" "+item['type']+",\n")
    output_h.write("""
    };
    """)

    Were able to throw out a quick enum:

    enum class list {
    none,
    tile,
    ticket,
    food,
    screwdriver,
    shovel,
    terminal,
    pickaxe,
    access_card,
    hat,
    corpse,
    fuel_brick,
    mug,
    oxygen_mask,
    urine,
    };

    My enthusiasm for this approach quickly snowballed. Not only was I now generating the enum and the static constexpr map from the JSON file, I added a column for the item descriptions, making them easy to read altogether and also amend, and also for the short descriptions used when the items are hovered over.

    Then I went one step further and moved a function from the engine client that selected an image to pair with each item, and had that generated from the JSON too. CastleDB has built in support for (varyingly-sized) tiles and will handily display them inline for easy glancing. I didn’t need the relative path to the image, both because resources in the _space_train engine come from specific named textures, and because the directory structure from compile-time has changed by runtime, but were that not the case it would have been easy to pull it out of the provided ‘tile’ data type in CastleDB.

    With this under my belt, I drove further forward and converted the floor tile types and image mappings too, as well as room bounds (which are done under the hood in an extra layer of tiles. Three whole compile units are now being generated! I made myself feel extra professional by abstracting common functionality, mainly related to the opening of files, into a python module called codegen.py. I refrained from getting too smart and making a special python class with scoped constructors and destructors which would function to emulate C++ namespaces and other cleverness like that, because down that road lies madness and the aforementioned template-based constexpr JSON library.


    Since adding the codegen functionality to _space_train I’ve identified several other candidates for static codegen, including a table of sound effects and a table of background music entries. If you made it this far, thank you for reading and I hope you enjoyed my terrible programmer jokes.

    If you want to look at the codegen Python in more detail and send me a message about how bad it is, it can be viewed in a pair of gists here (codegen.py) and here (item_codegen.py).

  • Morality and choice in Man of Steel

    This article transcribes a video essay available here, titled “Man of Steel is not objectivist.”


    The awkward truth, of course, is that we are all Clark Kent — there is always, for all of us, more we could be doing, risks we could be taking, effort we could be expending in the pursuit of being a better person. In the 2012 film Man of Steel, young Clark Kent is grappling with the knowledge that he has extraordinary powers, which he knows he ought to use to help people — given the opportunity. When his school bus crashes into a river, he swims it to the shore, saving everyone on board. A crisis is only narrowly averted; if Clark’s superhuman abilities were attributed to anything other than a mysterious ‘act of god’, his family life would be destroyed. Clark’s extraterrestrial origin is not compatible with a quiet upbringing.

    Clark’s dilemma is easily put: he did the right thing, but in doing so he put his family and all their lives in danger, so how can it have been the right thing?. But as he says to his father: “What was I supposed to do? Let them die?”

    His father responds: “maybe”.

    Some people think that Man of Steel is an objectivist film — objectivism being the mid 20th Century philosophy of Ayn Rand, a philosophy characterised by a radical selfishness and near-total rejection of the existing body of human philosophical thought. Like 80’s philosophical bitcoin. But people who think Man of Steel is objectivist are, to put it bluntly, wrong. And they’re wrong for many different reasons, but the one that interests me most is that — when I first gave it thought — it seemed obvious to me that Man of Steel cannot be an objectivist film because it’s so straightforwardly influenced by existentialist thought.

    The issue at hand is whether the questions raised by Man of Steel — questions including “what was I supposed to do?” are questions which reveal a moral flaw at the heart of the film. Is Zack Snyder an objectivist? Possibly. But Clark in Man of Steel would make a poor objectivist hero (At no point, it must be noted, does he consider going in to private enterprise). His villains (chiefly Zod), can’t be said to be collectivists or moralising philanthropists. They’re eugenicists, and Nazis. There is no recourse sought in the film to an objective moral code — in fact, this lack of an objective ethics is often cause for criticism of the film.

    Objective morality is represented best in the film by Zod’s Kryptonians, who are explicitly eugenicists.

    So, I want to talk through what I consider to be the ethics of the film: existentialist ethics. Man of Steel is full of scenes of anguished choice, absurd decision-making and bad-faith morality. As well, we’ll touch on the ethics of heroism in other action movies and then at the end talk briefly about the bottle city of Kandor and the bad faith of the Kryptonians. But first, this charge levied — that in picking at these moral scabs Man of Steel fatally undermines all morality — was indeed one faced by the existentialists in response to their focus on the actions of the individual

    This charge levied, that in picking at these moral scabs Man of Steel fatally undermines all morality, was indeed one faced by the existentialists in response to their focus on the actions of the individual: Simone de Beauvoir characterises the reaction against existentialism as saying that: “To re-establish Man at the heart of his own destiny, they claim, is to repudiate all ethics.”

    I have gotten ahead of myself.

    Clark saves the workers on the oil rig, but is paralysed by decision. Doing individual good deeds is not an end in itself.

    Existentialism is a philosophy of radical freedom popularised in the 19th and 20th Centuries. Well-known philosophers such as Simone de Beauvoir, Jean Paul Sartre and Soren Kierkegaard contributed to existentialist thought. Existentialism breaks from previous western philosophies in its emphasis on the radical freedom of the individual, in part derived from knowledge of the fixed inaccessibility or absence of a God — the idea that we are alone, and our whole lives have to be lived through without ever really knowing what morality “actually is”. Existentialism went out of fashion, both culturally and as a topic of philosophical investigation — but much of what existentialism says about human life and existence cannot be meaningfully said to be wrong.

    To paint in broad strokes, the existentialist says that a person is free: that though they may be influenced or pressured or predisposed, in the moment we each ultimately make the call, and there is no-one but ourselves to see into our hearts and evaluate the mode in which we made each decision. For Kierkegaard, who introduced the ‘leap of faith’ into the lexicon, our belief in God and our actions as such would be worthless, if they were not freely chosen in the face of a God who never responds. This absurdity is the true value of faith.

    In contrast, almost, much of action cinema and much of superhero cinema is, in being a power fantasy, often really about not having very much freedom at all — if you had to make agonising choices over which there were no clear correct paths, that might take a little of the fun out of it. The origin stories of superheroes are pressure cookers, running the protagonist through a life-altering series of reactions where the choice, when it is put, is more ceremonial in significance than taken in anguish. Spiderman, for example, is imposed on, first by the bite of the radioactive spider, then by the untimely death of his uncle, then by the threat the super-villains pose to his family and friends. He does have a famous great choice to make, where “with great power comes great responsibility” and so on and so forth, but the choice itself is not difficult — Peter has clear and direct moral guidance on this point, he was never going to turn around and say “actually, screw this”.

    When Peter’s morality does break down in Spider-man 3, it’s his personal morality, not that relating to his heroics.

    Similarly, Iron Man’s Damascene conversion from arms dealer to action robot is not one taken under introspection. His life as an arms dealer reaches a dramatic pre-ordained end: violence begets violence. His choice is between dying as an arms dealer or executing righteous justice on his jailers — some choice! And this is the power fantasy aspect of it — it’s a shared human fantasy, of being found in the righteous spotlight, compelled and justified in doing or saying something we’d otherwise feel ashamed of, because it’s something rude, or something disproportionate, or something illegal.

    Expressing these power fantasies in film isn’t necessarily a bad thing, either morally or in terms of making a great movie: in Assault on Precinct 13, a rag-tag bunch of individuals are forced by circumstance and morality into mounting a last-ditch defence of a police station against seemly never-ended waves of punk kids. To this end, they kill indiscriminately for most of the film’s runtime. Much of the thrill of the movie revolves around seeing — forced in the moment — whether or not the criminal characters, who are implicitly not part of the moral contract the rest of the characters adhere to, will in fact be morally compelled in this way.

    Of course, this pattern doesn’t fit every superhero, or every action protagonist. Aside from anything else, there’s just lots of them, and they’re used to tell all sorts of stories. Even Batman, for all that people like to fantasise about him as a power fantasy, is really just too weird a concept to be un-ironically identified with in that way. In Die Hard, Bruce Willis repeatedly choses to reintroduce himself into the sequence of events in a way which in the sequels will be transliterated into belief in his own hyper-competence, but in the original feels more like personal instability.

    Living the dream.

    Which brings us to consider again the condition of Clark Kent in Man of Steel. We see him drifting, taking several jobs. He saves the lives of men on a burning oil rig — but he also has a power trip on this petty asshole in the bar. We see that Clark feels obliged to use his abilities to do good in the world, but we see him struggle with translating that obligation into action. With great power comes great responsibility — in the abstract — but to who is this responsibility due, and how? Even in seemingly innocuous decisions like taking the job on the fisher boat, he causes one of his new colleagues to risk his life — just by existing, he is placed in situations where others feel obligations to him; there is no vacuum for him to perform idealised good deeds in.

    In this way, Man of Steel preempts this traditional introduction to the action protagonist, where we meet them on the cusp of an event that will transform them into a hero. Clark is already a hero in so much as he can be a hero; he has been a hero since he was a child and raised that bus from the river; but that doesn’t help him decide what to do right now. The issue that he questioned as a child is still entirely relevant: he knows that he ought to do that which is moral, but how to decide the most moral path? We can see in the opening sequence that he has become an ascetic, a nomad, a drifter. He has no extraneous human attachments that we see. His life is consumed entirely by these small moments of heroism and the balance of danger that comes with them.

    Existentialism has a word for the the difficulty of choice: anguish. Having to act, and so choose one of several paths which cannot be known to be the correct one beforehand. In Existentialism is a Humanism, Sartre says:

    “When a man commits himself to anything, fully realising that he is not only choosing what he will be, but is thereby at the same time a legislator deciding for the whole of mankind — in such a moment a man cannot escape from the sense of complete and profound responsibility.” — Jean Paul Sartre

    And this is the dominant mode in Man of Steel, from the bus rescue through to the climax. Clark cannot escape a sense of complete and profound responsibility. He must choose. For humanity, he chooses as a man to give himself up to the Kryptonians. As a Kryptonian, he chooses to abandon his people and their one cruel shot at restoring their former glory. And he receives no shielding from the consequences of these choices. His family’s lives are placed in danger. Hundreds of people die in collapsing buildings and explosions. To save humanity, he ends Krypton with his own hands.

    This breaks more than a fantastical “no kill” rule; it leaves Clark alone in the universe of his people.

    This is a painful choice to make, even though it is the right one. And so when, as a child, Clark demands of his father — who has taught him to be good, who has taught him: great power, great responsibility: what was I meant to do? Do I owe more to the lives I saved, or the lives of my family? Lives in the present, or the lives I will save as a grown Superman? What was I meant to do, let them die? There is no good answer Pa Kent can give. The choices are not going to get easier from hereon in. Clark has to consider — maybe he should have let them drown — because, as trivial as this particular case seems in the aftermath, choices are coming for which there will be no-one who can help. This is the lesson Pa Kent dies teaching Clark: not just that doing good will involve hard choices, but that doing good will involve unfair choices and ridiculous choices and hopeless choices. Clark will have to kill Zod, ending his entire race, because Zod refuses to stop senselessly killing. That is an absurd choice, and yet, morally, it is the right thing to do.

    I think people see objectivism lurking behind these thoughts because they make heroism fundamentally un-enjoyable. Seen in clear light, it stops being a power fantasy and starts being a burden no-one could possibly want. And so, presented with situations where the easy, power fantasy fulfilling option is the selfish one, they see a movie which wants them to be selfish.


    There’s a concept in the Superman comics called the bottle city of Kandor. The idea is that the robot Brainiac shrank an entire city from Krypton before its destruction, incorporating all of its culture and people, into a bottle, and Superman later fights the robot and retrieves the bottle. Then, aghast, he repeatedly fails to return the city to its proper size without causing some kind of corresponding catastrophe on Earth. This is generally interpreted as a ‘great burden’ type deal, the one line which even Superman is not powerful enough to cross. In Man of Steel, this concept is revisited in the codex, the dead sum of Kryptonian culture with which Clark is entrusted. The codex could be used — by Zod — to restore Krypton on earth — at only the cost of all of humanity.

    A bottle city…

    The Kryptonians, before the destruction of their world, are portrayed as the beneficiaries of eugenics: the codex is used to ensure that every Kryptonian is bred exactly for his role, and no new life is permitted outside of this. In this way, the character of every Kryptonian is drawn into the codex. They are defined fully before they even exist, and so restoring Krypton is simply a case of calling them back into being.

    I think the transition from the bottle city to the codex, which is literally a skull, makes explicit the ghoulish quality of the exchange: to trade in the living for the dead, an ascendent culture for one which has already served to extinct itself. Between the codex and Zod, the villains in Man of Steel are not the foes of objectivism, but the foes of existentialism. In the codex, the idea that a people can have their worth defined prior to their choices in life, and in Zod the exemplar of a man who attributes all his successes and failures to preordained destiny.

    …and a skull.

    Zod’s account to Clark, of his inbuilt, fixed character and quality, is a fantasy. Zod claims himself as born a military leader, never deviating from that path — even as he commits the act of rebellion. He rejects that he made that choice, and he rejects that he made the choice to kill Jor-el, even though he visibly does so in anger. At the end of the film, when all hope is lost, he claims that his every action, no matter how violent, exists to serve Krypton —

    “I exist only to protect Krypton. That is the sole purpose for which I was born. And every action I take, no matter how violent or how cruel, is for the greater good of my people. And now I have no people. My soul. That is what you have taken from me.”

    — but how, then, does he justify his final actions, once Krypton (calcified in the form of the Codex) has been destroyed? Zod is choosing, even as he claims not to choose. Zod is thus the mirror counterpart to Clark, the actor in bad faith, denying his choices even as he makes them.

    “The genius of Proust is the totality of the works of Proust; the genius of Racine is the series of his tragedies, outside of which there is nothing. Why should we attribute to Racine the capacity to write yet another tragedy when that is precisely what he did not write? In life, a man commits himself, draws his own portrait and there is nothing but that portrait.” — Jean Paul Sartre

    The Kryptonians and Kryptonian society want to deny that, as individuals, they are free, and also that, as individuals, they are abandoned. Kryptonians have their behaviour determined and implicitly excused by their genetic programming. The worth of any given Kryptonian is privileged to be the worth of all Kryptonian society. In this way, from this vantage point, Krypton demands the opportunity to write another tragedy — Zod demands the opportunity to write another tragedy. And because he is attributing his desires and choices to this external source, denying that he has influence over them, he denies himself any end other than the one he receives: stripped of all that provided him meaning, he acts entirely out of spite and malice.

    Zod strikes out of anger, already defeated.

    Ayn Rand defines her philosophy, in the appendix to Atlas Shrugged, like this:

    “My philosophy, in essence, is the concept of man as a heroic being, with his own happiness as the moral purpose of his life, with productive achievement as his noblest activity, and reason as his only absolute.” — Ayn Rand

    I dunno, your mileage may vary, but I don’t really see any of that in Man of Steel. Clark certainly doesn’t see his own happiness as the moral purpose of his life in any way, and his two father figures both sacrifice themselves for their faith in a nameless future. In one of the most famous quotes from Existentialism is a Humanism, Sartre defines in contrast the existentialist creed: “man first of all exists, encounters himself, surges up in the world — and defines himself afterwards”

    And this, I think, is the essence of Man of Steel. Clark defines himself.

    To return in the last to de Beauvoir, arguing against the criticism that existentialism is a philosophy of what might be called moral relativism:

    “far from God’s absence authorising all license, the contrary is the case, because man is abandoned on the earth, because his acts are definitive, absolute engagements. He bears the responsibility for a world which is not the work of a strange power, but of himself, where his defeats are inscribed, and his victories as well. A God can pardon, efface, and compensate. But if God does not exist, man’s faults are unforgivable.” — Simone de Beauvoir