# Tiny BASIC

Interpreter and Compiler Project

# Kingdom of the Lyre: Player Actions

Friday, 15th November 2019

In this blog series, Damian discusses how the game works.

So far in these posts on Kingdom of the Lyre, we've looked at what the game does to the player: the world created, the random encounters generated. Player actions have been limited to the choice of fight or flee. But once that's out of the way, the player gets to choose a path. And there we return to path generation. Let's take a look at the player action subroutine:

```    REM --- Subroutine to deal with player actions
REM --- Changes: D - user input for direction
REM ---          E - exits from location
REM ---          N - new player position
REM ---          P - player position
REM ---          Q - quit flag
260 LET N=P
LET X=P
GOSUB 350
REM --- check if the underworld forces are near enough to hear
LET X=U
GOSUB 600
IF A<16 THEN GOSUB 740
IF A<16 THEN GOSUB 730
REM --- check if the lyre is near enough to hear
IF L=O THEN GOTO 261
LET X=L
GOSUB 600
IF A<8 THEN GOSUB 700
IF A<8 THEN GOSUB 720
GOTO 262
REM --- We're on the way back to the village
261 LET X=O
GOSUB 600
PRINT "Your home lies ",A," leagues"
GOSUB 730
REM --- Ask direction and calculate new position
262 PRINT "Direction? 2=S 4=W 6=E 8=N"
PRINT " or: 0=quit 1=drink"
INPUT D
IF D=0 THEN LET Q=1
IF D=1 THEN IF I<>0 THEN GOSUB 360
IF D=2 THEN IF E/2-E/4*2>0 THEN GOSUB 370
IF D=4 THEN IF E/4-E/8*2>0 THEN GOSUB 370
IF D=6 THEN IF E-E/2*2>0 THEN GOSUB 370
IF D=8 THEN IF E/8>0 THEN GOSUB 370
IF N=P THEN IF Q=0 THEN GOTO 262
LET P=N
RETURN```

There are a lot of GOSUBs here but let's stick with this routine that shows the bigger picture. The subroutine at 350 just prints the directions that the player can travel. The next two parts of this routine look to see what we can hear from where we are. If we're close enough to the underworld army, we should hear them or see peasants fleeing. If we're close enough to the lyre, we hear its magical music and need no hints from random peasants. In the case of the lyre, the player may already have it (when L=O), so instead they are told where their home village is.

All this taken care of, we can actually ask the player where they want to go. The directions available have already been printed. The menu shows all the controls, and then calls the various subroutines for enacting them.

Option 0 is simple enough, and just sets the Quit variable. Option 1 goes to subroutine 360 to drink a potion. Here it is:

```    REM --- Subroutine to drink a potion
REM --- Changes: I - potion in hand is removed
REM ---          J - potion in effect is updated
REM ---          K - potion duration is refreshed
360 PRINT "You drink"
LET X=I
GOSUB 710
LET J=I
LET K=10
LET I=0
RETURN```

Subroutine 710 just prints the name of the potion. Notice that if a potion is already in effect (J and K), its remaining effects are discarded.

Now returning to the menu at line 262 in the item above, notice the complicated IFs that decide both whether a player wants to and is able to go in each of the four directions. The Exits variable E is a combination of four values, 8, 4, 2 and 1 for North, West, South and East respectively. Tiny BASIC has no bitwise AND operator to isolate these values, so these long-winded arithmetic expressions are needed to do the same job. Sub-expressions like E/2*2 rely on the fact that integer rounding will take place, removing the remainder of the division before multiplying back up again.

In the next and final blog post I'll cover the movement of the forces of the underworld, and close with some general points about how the code is arranged, and how it can be improved.