This page is a collection of code bits that I find useful in my games. Some of them
were written by me, others were not. All are marked accordingly. They range from short
and simple to long and rather complicated depending on what they are supposed to do.
For more code try looking at the TADS
Example Code page.
Improved Random function
When you're writing an interactive fiction game you often want a randomly generated response.
Easy enough, just use the provided rand() function, or if you want use _rand() instead for a
slightly more random number. The problem with both of these is that very often you only have
maybe five or ten possible responses. A call that looks like rand(5); could easily return something
like: 5, 5, 5, 2, 4, 1, 3, 3, 2, 2, 2, 2. Okay, so it's random. It isn't useful though. If your
game randomly generates the same repsonse three times in a row it isn't going to make you look
very good. So what we really want is a semi-random function. A function that will randomly
generate a number but not repeat the last randomly generated number. This piece of code does that.
It will randomly generate numbers and never repeat more than once every three generations. Just
include this code in your game and call srand(); instead of rand();. I call it srand for "small
random" use it when you have a small number of possible responses and don't want them to repeat
often.
Destruction Module
If you add an axe or a box of matches to your game you don't want to go through the entire game
defining what can or can't be burned and what can or can't be broken into tiny bits. You want the
game to figure it out for itself. This code does that. In fact there is at least one solution in
The Erudition Chamber that I didn't even know existed (and certainly should exist) until a player
pointed it out to me, that is a direct result of this code. This is a complicated bit of code though.
It is going to take some studying on your part in order to figure out how to use it. The code is
fairly straight forward but I have a tendency to comment sparingly so keep that in mind when you
decide to use it. If you try to use this code without fully understanding everything it does your
game will probably break. You have been warned.
Singular Plurals
This is an excellent piece of code that I didn't write but use often. If you have a pair of scissors
they are a single object but they should be refered to as a plural. This code takes care of that and
prevents the somewhat odd messages that would be returned if it wasn't included. Rather than explain
it in detail I'll just refer you to the comments that the author included at the begining of the file.
Disambiguation Code
This is another extremely useful bit of code that I did not write. There are many cases that come
up while writing a game where you would like two objects to have the same nouns/adjectives. The result is
that if the player types that word they get asked which of the two or more items they are
refering to. That's fine but it can get old from the player's point of view and sometimes it should
be obvious which of the objects they are refering to. In such cases this code allows you to
automatically determine what the player means and respond accordingly. For a more indepth description
of what this code does see the comments at the begining of the code.