I mostly despise jetlag but once in a while manage to harness it for something positive. I remember some great trips to Asia, waking up at strange times to experience the dawn, walking the streets and seeing everyday life starting up around me. These days I typically find jetlag to be a bit of a curse, but this weekend I ended up using it to tackle a fun challenge: generating maps for Z-machine games such as Zork inside AutoCAD. Something I mentioned in a recent blog post but was reminded of last Thursday while catching up with Christer Janson in San Rafael.
You can find a number of Zork maps online. My favourite is from the mainframe version (by all accounts it may well be the only that exists from that era):
While I didn’t expect to recreate anything as beautiful as this, I did want to let people capture the essence of the games they play, and provide them some flexibility to control the layout of the map. My original, abortive attempts at mapping Zork automatically were based on the assumption that Zork’s map was perfectly recto-linear (which it absolutely isn’t).
I’d already decided I needed to use a graph – rather than a grid – to map Zork, but had stalled when I realised I didn’t have a way to identify locations uniquely: many have the same names and some also get described differently if you’ve been there before. When I chatted this over with Christer – who had spent many years playing and mapping Zork, as a teenager – he suggested hacking the Z-Machine VM to expose a unique identifier (which certainly exists in the file format) for the current location. This proved to be straightforward to do and worked a treat, but meant I also then had to tackle the bigger problem of getting the rest of it to work. Thanks, Christer! ;-)
I knew I wanted to use a JavaScript library for the graph management. I’d thought Cola.js might be a candidate but ended up settling on Cytoscape.js: Cola.js is just one higher-level layout library that works with Cytoscape.js. We’ll see whether it makes sense to use Cola, too, or something else.
It ended up being straightforward to integrate Cytoscape into AutoCAD: I chose to use a palette, rather than a document window, as this sets us up nicely for bringing the geometry across into AutoCAD (something we’ll see in another post).
Here’s the palette in action – expanded to the extent of the video recording so it’s nicely visible. Right now it’s launched using a separate command – ZMAP – but could also be launched by the ZORK command directly.
The graph is mostly laid out relative to the points of the compass, but you can position nodes manually as you go. When you come to a location that already exists in the graph, its node will get highlighted and subsequent new nodes will be positioned relative to that.
Here’s a static image of a more extensive graph I mapped over an additional minute or so:
The code is basically ready to share but I’m going to finish up the “Export to AutoCAD” capability before I post it here. It’s unlikely I’ll go the extra step to make the code create associative geometry, but it will certainly show you how it’s possible to bring a snapshot of data across from a graph layout system such Cytoscape into an AutoCAD drawing. Which will be interesting for people wanting to implement this kind of workflow (beyond this somewhat narrow – albeit super-fun – scenario :-).