Previous
Top
Next
The objects in an adventure game, like the rooms, are
represented by an
array of
structures. Each object has
a structure which contains the following information:
- A short name for the object.
- A longer name for the object.
- The object's location (room number).
- Whether or not the object may be carried.
The following C structure may be used for this:
struct item_t { /* one for each object in the game */
char *short_name;
char *description;
int location; /* index into room array */
int portable; /* can be carried? */
};
The short name should be the single word by which the player
will refer to the object.
In a more sophisticated game, there might be a "type" field
which identifies if the object of some special type, such as
a "door", which may be opened, closed, locked and prevent
or allow travel between rooms, or perhaps a container which
may contain other objects. Additionally a void * pointer
may be used here to point to a special purpose structure to
hold the properies associated with these special objects. For
ordinary objects that pointer would be null. For "door" type
objects, it would point to a kind of structure that contains
information peculiar to doors, (opened or closed, locked or
unlocked, what rooms is it between, etc.) and for "container"
objects it would point to a structure that contained the information
specific to that container, etc.).
I'm not really going to go into too much more detail about those
types of things here though, just a taste.
So all this is pretty straightforward, just strings for the
descriptions, and an integer for the location. So, what about
objects which the player is carrying? What room are they considered
to be in? Well, just pick a number outside the range of room numbers.
Traditionally, the value of zero is used, and the zeroth element
of the room array is just burned up for that purpose. The player
cannot usually accidentally wander into room zero (his own pocket)
due to the simple fact that there are no travel table entries that
lead to room zero.
However, there's nothing magic about zero. It
could easily be argued that the value -1 makes more sense. One
advantage of using zero over -1 is that
negative values may be conveniently used to indicate relative
position of two objects such as "insideness", or "on-top-of-ness". For
example to represent that an apple is "on the table", the apple might
have the negative value of the table's object number as it's
location. The program would know that the apple has some relation
to the table because the location is first of all, negative,
and therefore not a room number, and it is the table's object number.
It would know that the apple is "on" the
table and not "inside" the table because it would know that the
table is a "surface" type object,
and not a "container". This simple scheme
breaks down for objects which may be multiple types. (E.g. a large
sea chest could serve as both a table and a container.)
That limitation may be overcome by assigning each relationship
type a small integer, starting with zero, and advancing upwards
by one. For example, Suppose we have:
Object Object number
-----------------------------
apple 21
table 37
total number of objects in the game: 154
relationship relationship number
-----------------------------------
"inside" 0
"tied to" 1
"bolted to" 2
"on top of" 3
Now, to represent that the apple is on the table, what
value could we use for the apple's location?
Well, How about the negative object number of the table
minus relationship number times the total number of objects
in the room?
something like:
int apple=21, table=37, on_top_of=3, total_objs=154 ;
object[apple].location = -table - (total_objs*on_top_of);
This formula gives a unique value for the location for
each combination of the two objects and a relationship, taking
into account that the order of the two objects is important.
("the apple is on the table" is not equivalent to "the table is
on the apple"). It's a fairly straightforward operation to
decode this value to find what it means. Use the modulus
operator ('%' in C) with the total number of objects to find the
object number, and truncating integer division to get the
relationship number.
Ok, that's it for the objects in our adventure game.
Next up, tying it all together with a parser.
Previous
Top
Next