nwn.ss - S-Expressions for NWScript

Neverwinter Nights and its sequel use a custom scripting language called NWScript. Why Bioware didn’t use something like Lua, I’m uncertain, but reinventing the wheel isn’t exactly a rarity in the software industry.

NWScript is very much like a cut down version of C. Unfortunately, it lacks arrays, or any form of list-like data structure. This makes designing complicated scripts in it rather trickier than it otherwise would be.

To this end, I’ve created nwn.ss, a PLT Scheme module for turning a custom S-Expression into valid NWScript.

For example:

(nwscript
  '(def ((object FooBar))
    (switch (Random 3)
      ((0) ((return (CreateItemOnObject "foo"))))
      ((1) ((return (CreateItemOnObject "bar"))))
      ((2) ((return (CreateItemOnObject "baz"))))))

Gets turned into:

object FooBar()
{
        switch (Random(3))
        {
        case 0:
                return CreateItemOnObject("foo");
                break;
        case 1:
                return CreateItemOnObject("bar");
                break;
        case 2:
                return CreateItemOnObject("baz");
                break;
        }
}

The advantage of writing the code as an S-Expression is that I can remove all the boilerplate code that’s necessary in a language like NWScript. I can write a function to generate the appropriate code for me. In some custom code I’m writing, I’ve created a def-random-item function that makes creating functions like the one above trivial:

(nwscript
  (def-random-item 'Foobar "foo" "bar" "baz"))

If you’re a Schemer or Lisper who plays NWN, you may be interested in this. It’s currently alpha quality though - I’ll need to use it a bit more than I have before I’m sure I’ve worked out all the bugs :)