Maya Grab [Day 20] – Interpreter (2) and game statistics

More than a week passed since the last journal entry, but I was everything, but not lazy. Lots of work has been put into this version because I wanted it to be the first fully playable alpha. Along with constant low memory issues, the interpreter is now completely finished, lots of optimization and bug fixing has been done, and game statistics are added as a new feature which is not available in the original game. Let’s see the code changes.

5140 PT=TIME/60 : REM RESET TIMER

16515 A=INT(TIME/60PT)
16520 X=INT(A/60): Y=INT(AX*60)
16525 X$=STR$(X): Y$=STR$(Y)
16530 X$=RIGHT$(X$,LEN(X$)-1): Y$=RIGHT$(Y$,LEN(Y$)-1)
16535 IF X<10 THEN X$=“0”+X$
16540 IF Y<10 THEN Y$=“0”+Y$

When a game starts, the system time in seconds is stored into the PT variable. Then when the player calls the statistics, it’s subracted again from the system time. The seconds and minutes are calculated from the given number and then formatted and converted to a string. The overall progress is calculated by adding points for every room visited (1 point), and for every solved puzzle (1-5 points). It’s a bunch IF THEN calls which are checking every single room flag, and it’s a best example how not to organize data. All the room states could be simply stored like a integer array, one entry for each puzzle. It would perform faster, take less memory, could be tested in a loop for the statistics part. It’s first on the list for refactoring in future versions.

– check for a special command
  these commands are: statistics and new game
– check for look at object command
  only objects that the player has picked up are processed
– check for room specific commands
  is command matching one of the commands for the room the player is in
– check for priority commands
  these commands are: take, go, look, open
– check for spaces in user input
  are there any spaces, if yes extract first two words from user input
– check length of the entered words
  all known words are not longer than 10 characters
– check if entered words are on the known words list
  both lists verbs and nouns are processed for first two words

This is how the interpreter works. Here it’s not about being smart and handle as many words and combinations as possible. It’s about speed and giving the player as soon as possible a meaningful response using BASIC on a C64. After many hours spent testing the whole thing, this is a more-less reasonable solution I’ve came up with, and I think it’s performing a bit slower than the interpreter in the original game.

– execute a special command
  prints the game statistics or restarts the game
– room specific response
  one of the responses for the room the player is in
– priority command response
  can’t take/go/look/open that now
– known words response
  can’t do that now
– known word response
  what?
– words longer than 10 characters, or unknown words response
  “word” – don’t understand

These are all possible responses from the interpreter. If you really want to test your patience, just enter a 30 character sentence with first two words under 10 characters and not from the known words list. For speeding things up I’ve stored the priority commands (with one space at the end) in separate variables (lines from 3980-3983); it saves many string concatenations. Also the get input routine was extremely slow because it’s located around line 14500, and the play sound subroutine at 51000; I’ve relocated it to line 14950.

Program size: 38688 bytes, on disk 36830 bytes or 145 blocks
Free BASIC memory: 223 bytes
Used BASIC commands: TIME

Source code BASIC
Source code ASM

* * * * *