REALMS OF INTERACTION
part 2 of 5
by Alan Davis
from ZX Computing, June 1986

This month Alan Davis starts to build up a world for your adventure
characters to inherit.

Last month we looked at some of the advantages - and problems - of
writing adventure games set in an imaginary world which is "inhabited"
by computer-controlled characters - characters which give the illusion
of leading their lives independently of the player. In this second
article in the series we're going to put together a simple world for
our characters to inhabit, and also come to grips with some powerful
string handling routines. There's quite a lot of ground to cover, so
let's get straight down to business.

Listing 1, together with the "z$print" machine code routine from last
month's article, forms what we might call the basic program module for
the series. In fact more than half of Listing 1 is concerned with
setting up the data and defined functions we'll need in order to bring
the independent characters to life in later articles. To get the
program running, enter CLEAR 59999 as a direct command, and type in
Listing 1. DON'T attempt to run this yet! load in the "z$print" code
you saved last month, and then type: GOTO 9998. The program will save
itself, and the machine code, to tape.

Rewind the tape, NEW the Spectrum, and load in the program (which will
autorun from line 8000, loading the bytes automatically.) After a few
seconds you'll find yourself in control of our good friend Merlin the
Magician, with six single key commands at your disposal. The N, S, E
and W keys will turn Merlin around to face the appropriate compass
direction, with the text description reflecting any changes in the
view he sees. The M key will move him one location in whatever
direction he's currently facing. The I key - for Inventory - won't do
anything at this stage, and neither will Merlin encounter any other
characters or objects as he moves about. This is all as it should be:
we're lacking three short but crucial machine code routines which
we'll deal with next month.


Map of the world

If you can tear yourself away from your new friend for a minute or
two, we ought to take a look at Listing 1 in more detail. The various
REMs should help you to find your way around the program, but the
following comments may be useful:

1) All printing of text to the screen is achieved by first building up
the message in the BASIC variable z$, and then using the "z$print"
machine code routine from last month's article. The subroutine at line
100 adds a full stop to the contents of z$ before making the USR call
at 64505. Incidentally, USR 3582 is a call to the Spectrum ROM for
scrolling the screen upwards by one line.

2) Lines 200-300 prompt for a single keypress and decode it (if
acceptable) into a number between 1 and 6 inclusive, stored in the
variable v; v = 5 corresponds to a movement command, and v = 1, 2, 3,
or 4 to a rotation command. The "current direction" is held in
variable d. The inclusion of a real time feature might seem pointless
at present, since nothing ever happens! Never fear, things will change!

3) Movement occurs within a 5 x 5 grid which forms a "world" of 25
locations. Variables io and jo store the two coordinates which define
Merlin's position within this grid: io = 1 represents the furthest
north that Merlin can go, and io = 5 the furthest south. Similarly jo
= 1 marks the western limit, and jo = 5 marks the eastern edge. The
variables ti and tj are used as flags to prevent the program trying to
describe an "impossible" location which is off the world map (see
lines 1020, 1030), and to enable it to describe the location directly
ahead when this is within the map.

4) The array l(5,5) effectively stores the map of the world, whose
layout can be seen in the DATA statements for lines 9530-9534
inclusive. Each element of the array stores a numeric code (between 1
and 5 inclusive) corresponding to one of the location descriptions
held in the array l$(). For example, if io = 2 and jo = 1, then
l(io,jo) = 4 (see line 9531). So this location corresponds to the 4th
item in l$(): "a thatched cottage".

5) Character names, objects, location names, and compass directions
are held in the arrays p$(), o$(), l$() and d$() respectively, each
string of text being preceded by either one or two character codes.
The job of extracting information from these arrays to generate text
messages is performed by the various functions defined in lines 10-40.
The purpose of the character codes is to define the length of the text
message to be extracted, and so eliminate unwanted spaces.


Demo routine

It may not be immediately obvious just how these defined functions
work, and you might find Listing 2 helpful in this respect. (Listing 2
is purely a demonstration routine, and isn't intended to be a
permanent part of the basic program module.) Once you have the main
program running (and a copy saved on tape), BREAK, and add the program
lines in Listing 2. Then enter GOTO 9800, put your feet up, and all
will be revealed! The demonstration will show you, far better than
could be explained in words, exactly what each function does. And at
the end it will show you the power of these functions by generating a
wide variety of text messages for as long as you have the stamina to
keep pressing a key! Actually, a careful study of lines 9920-9940 will
repay the effort, since these program lines are entirely responsible
for the remarkable range of text messages generated in the demonstration.

Of course there's no logic in the messages generated by the
demonstration program - that will come later, though you can see
examples of the system in use in lines 1000 and 1030. However, it's a
good idea to become familiar with this method of constructing sensible
messages from their various bits and pieces as we shall be using it a
good deal in the remaining articles. You might like to try rewriting
lines 9920-9940 to generate a different set of text messages.

Meanwhile, what about poor old Merlin? You've probably found that he
rapidly wearied of wandering about an apparently empty world. Tell him
not to worry. Next month will bring a population explosion ...

