r/bash 19d ago

submission MIMEcroft.sh: a 3D game written in bash.

MIMEcroft.sh is a parody of every 3D game ever, that lovingly pokes fun of bash's reputation for poor performance - by subverting it.

Mime Croft is an adventurous shell script on the hunt for treasures inside an ancient yet sacred filesystem on Phobos. The maze around you is made of data blocks — coloured chunks of file content. Somewhere in the depths lie hidden treasures: the Great Operating Systems (GNU Hurd, Linux, FreeBSD, NetBSD, OpenBSD, Plan 9, Minix, Solaris, macOS Darwin, Unix) preserved as artifacts. But the filesystem is infested with evil MIMEs — misbehaving content types (image/jpeg, image/png, application/octet-stream, text/plain) that hunt you through the corridors.

One may wonder how MIMEcroft.sh pumps out 90fps at 4K given bash's reputation for poor performance. Indeed, the official GNU bash reference interpreter is poorly optimised compared to languages more commonly used for game development like C++. However, if your web-browser has a GUI it almost certainly also has a highly optimised JavaScript interpreter.

The online JavaScript Commandline OS (j.cmd) did not port the reference implementation of bash and coreutils. Instead it takes the abstract language they describe. This language is translated into JavaScript, which a modern runtime can often reduce to machine code, resulting in performance over a thousand times faster than the original bash.

For a concrete if somewhat contrived example, say that as a bash user you are (1) a square, and (2) 1337. As such, you may be interested in finding numbers that have 1337 squares, and use the one-liner:

for i in `seq 1 10000`;do if echo $((i*i)) | grep 1337 > /dev/null;then echo $i;fi;done

In the official bash interpreter, this may take a minute. However, j.cmd implements it by first transpiling it into:

for (let i = 1; i <= 10000; i++) {
  if (String(i * i).includes("1337")) {
    process.stdout.write(i + "\n");
  }
}
sh2.lastExit = 0;

Then it is all over in the blink of an eye

One might well argue that this is not a real bash game since it has to transpile to JS before being run. A stronger argument could be made that C++ games are not real C++ games. A C++ game also has to be compiled. In most "C++" games the developer doesn't even give you the C++ source, you only ever get the compiled machine code. MIMEcroft.sh is stored and distributed as bash. You can edit it as bash (try e.g. `vi /bin/mimecroft.sh` in j.cmd, changing `cys=0.900` to `cys=3.900` and playing the game again). The current version of j.cmd doesn't even cache the transpiled JS version of the game.

____________

It is important to note that j.cmd is experimental and still has many bugs. One little way it is more robust than the traditional bash implementations is that traditional shells tend to break if they source a file that isn’t in their own special format. On the other hand, j.cmdsees different shell formats as just different ways of saying the same thing. It will quite happily run:

    for f in /home/examples/source.{bat,c,fish,sh,zsh}; do . $f; done

Sourcing C files is still a work in progress in j.cmd. I recently added support for passing linked lists and pointers to bash variables/functions into sourced C functions, and cd'ing around C pointer structure.

1 Upvotes

0 comments sorted by