Following on from the intro and scene setting of my previous post, this is the first in a series of XC=BASIC v3.0 features on various snippets of code and features for my Adventure Game template.
The idea is to create a framework for the working engine of a text based adventure game, including most of the generic parser routines and data structures for room, objects, inventory and actions etc.
My current thoughts are to include Excel in my IDE as a tool to enter, in array sequence, room and object names, descriptions, examine properties and exits and write a macro to turn these into XCBASIC statements. I may change this to write the generation of the code on the C64 itself for completeness, but my goal is not to create an adventure writing system per se, such as The Quill and others, but a framework of code I can re-use and adapt going forward.
Anyway, with all the benefits of XCBASIC, and there are many, there was one glaring omission which I think is vital for text based games, and that is the CBM Basic function of INSTR(). I come from an Oracle DBA and PL/SQL background and to me it’s an essential function.
Type: INTEGER Function General Programming-Syntax: INSTR(<string 1>, <string 2>[,starting-position])
Having recently logged an issue on the XCBASIC GitHub page, I spent a few minutes browsing through other listed issues/requests and was happy to see INSTR() included as something for future release. But in the mean time I had already written a small subroutine to achieve what I needed for the framework, which is what I’m discussing here.
So, the above subroutine is fairly straightforward and should be easy to follow through. It is called an awful lot from my game framework and it is vital for the parser to allow it to be forgiving in user input. We not only want to pick up ‘the old and dusty lamp’ we want to be able to pick up ‘lamp’ or ‘the lamp’ or ‘the old lamp’ or ‘the dusty lamp’ – these individual words are captured in their own string variables and tested using INSTR() to see if they exist in the room object array. In most cases we are only interested in the noun and not the adjectives….but not always.
Let’s quickly go through the INSTR() subroutine above:
After defining the SUB and its arguments on the first line, along with their data types, the next 2 lines simply initialise a few variables.
M holds either 0 or 1. 0 means No Match Found and 1 equals, you guessed it, Match Found. V is just a loop counter variable. L1 and L2 hold the lengths of both the string to test, T$, and the string to check, C$.
With V now being zero the loop will start and will continue until V is less than the length of C$ minus the length of T$ + 1 i.e. we have reached the part where the word we are looking for could not possibly fit into the string we are checking.
Each iteration then simply checks if the string at starting point V and of length L1 characters matches the string we are testing, T$. It then increments V.
So with M initialised in this SUB we can simply test for the value of M when it returns to the calling block of code. Like I said, nothing complicated and fairly easy, and there may be more efficient ways of doing it.
Remember these posts are not instructor led, I am not an expert by any stretch of the imagination, they are on my part aspirational and hopefully collaborative to some degree, and as we delve further into XCBASIC I hope they will serve as a repository of useful information for whoever stumbles across them.
Until next time!