Maya Grab [Day 6] – Defines and data initialization

Well, it was about time to write some useful code which actually does something. Today’s task was to write all variables for constants and some of the game data into the code. Variables on the C64 take up 7 bytes of memory and, of course, you can’t have dozens of them in your program. I remember once, I wanted to do a strategy game and didn’t have a clue that arrays exist (yes, I was 9 years old), so I’ve stored lists like separate variables. Epic fail; I’ve got the “out of memory” error really really fast.

Anyways, constants must be defined for addresses which are going to be read (PEEK-ed) or written (POKE-ed) very often; it makes the code much faster. Also, I’m going to use constants for some colors and screen positions for drawing things. There are also game constants (room data, interpreter data… etc) which aren’t going to change during gameplay and should be initialized only once. And last but not least there are variables which are going to change often. Unfortunately, you can’t give a variable name something like “MYAWESOMEVARIABLE”; well you can, just tried it out, but that’s going to eat up your whole screen space, and we said max. 39 characters (including the line number) for optimal code beauty. Therefore I cut down all variable names to a max of 3 characters. Let’s take a look what I have written down so far:

V=53248: S=54272

The first one is the address of the VIC2 graphics chip which handles, among other things, sprites. The second one is the address of the SID sound chip.

MLA=780: MLX=781: MLY=782

At those addresses the last state of the three CPU registers (accumulator, x and y) is stored. This is useful for passing data to the machine language code or vice versa.

GCB=12: GCF=0: GFW=27: GFH=19

The first two are colors of the border and the foreground and the last two are dimensions of the frame in which the current room is drawn.

GPO=1: GPD=11: GPI=20: GPM=22

Y positions on screen where objects (GPO), directions (GPD), user input (GPI) and messages (GPM) are drawn.

SFL=100: SFH=200

Frequencies for playing sound for typing (SFL) and confirming (SFH) a command.

PIS=16: RMN=20

Max number of items in players inventory (PIS) and total number of rooms (RMN) in game (including title and RIP screens – like Ron Gilbert did in Maniac Mansion).

DIM ROA$(RMN,6): DIM RDA$(RMN,6)
DIM RMA$(RMN,16)

Two dimensional arrays where objects (ROA), directions (RDA) and messages (RMA) are kept for every room.

DIM IMA$(16): DIM IVA$(35)
DIM IAA$(40): DIM INA$(66)

One dimensional arrays where messages (IMA), verbs (IVA), adverbs (IAA) and nouns (INA) are kept for the interpreter.

PRN=0: PGT=0: PIC=0

Player variables for holding current room number (PRN), game time (PGT) and number of issued commands (PIC).

DIM PIA$(PIS): DIM RSA(RMN)

One dimensional arrays for holding players inventory (PIA) and room states (RSA).

A%=0: X%=0: Y%=0
A$=“”: X$=“”: Y$=“”

Here I wanted to define couple of helper variables for passing parameters to and returning from subroutines, and since the CPU has three registers, I defined three integer and three string variables and gave them same names just for fun.

Program size: 7759 bytes, on disk 5080 bytes or 20 blocks
Free BASIC memory: 31152 bytes
Used BASIC commands: VARIABLE, DIM

Source code

* * * * *