# Tiny BASIC

Interpreter and Compiler Project

# Kingdom of the Lyre: Encounters

Wednesday, 13th November 2019

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

Encounters in Kingdom of the Lyre have a different type of complexity to map generation. They need to change during the game: if you kill the three orcs at the mountain pass, you don't want to see the same three orcs a moment later. On the other hand, if you don't like the look of a crocodile that guards the entrance to the swamp, you shouldn't be able to hide around a corner and expect it to be gone a moment later.

These are casual encounters. There are other types, too. The object of your quest is an encounter, there might be villages, and there is the main enemy who relentlessly chases you. The first subroutine I want to look at today is short, and decides which of these encounters should happen at a given time.

```    REM --- Determine possible encounter types
340 LET V=0
IF P=L THEN IF P<>O THEN LET V=2
IF V=0 THEN IF P=U THEN LET V=3
IF V=0 THEN IF P=L THEN IF P=O THEN LET V=4
IF V=0 THEN IF P=O THEN LET V=1
IF V<>0 THEN RETURN
REM --- All predictable encounters eliminated - check villages
LET Z=4883
LET C=0
341 GOSUB 990
LET C=C+1
IF C<=P THEN GOTO 341
IF Z-Z/20*20=0 THEN LET V=1
RETURN```

The first part of the subroutine deals with the "predictable" encounters: have we found the lyre? Has the army of the underworld found us? Have we returned the lyre and won? Are we otherwise at our home village? If the answer to all these is "no", then the second part of the subroutine checks to see if there's a random village here. If there is no village, then that leaves us with the default (V=0), which is a random encounter. These are determined by this subroutine:

```    REM --- Deal with a random encounter
210 LET Z=F
LET C=0
211 GOSUB 990
LET C=C+1
IF C<=P THEN GOTO 211
IF Z-Z/8*8>1 THEN RETURN
REM --- Monster encountered
GOSUB 400
IF J=3 THEN GOTO 213
LET Z=G
REM --- Combat loop
...```

While path, terrain and village generation use set seeds (4881, 4882 and 4883 respectively), random encounters use a variable seed, stored in F. This is generated at the start of the game, from the 4-digit number the player was asked for. It is only changed when the player wins a battle.

The effect of this is that if the player evades random monsters by fleeing or drinking an invisibility potion, then those monsters will stay where they are. But if a player fights the monsters and wins, then they will disappear and others may or may not take their place. A side-effect is that all other random monsters will also move around at that time, but that is hopefully not too noticeable.

Line 400 is the subroutine that generates the details of the encounter and displays them:

```    REM --- Subroutine to generate a monster encounter
REM --- Inputs:  T - the terrain
REM --- Changes: Z - random number seed
REM --- Outputs: M - the number/strength of the monster(s)
400 GOSUB 990
LET M=1+Z-Z/5*5
GOTO 401+T
401 IF M=1 THEN PRINT "There is a thief here."
IF M>1 THEN PRINT "There are ",M," thieves here."
RETURN
402 PRINT "You see ",M," wild boar emerge from the undergrowth."
RETURN
403 IF M=1 THEN PRINT "You encounter a goblin in the mountain pass."
IF M>1 THEN PRINT "You encounter ",M," goblins in the mountain pass."
RETURN
404 PRINT "You are stalked by a hungry crocodile."
RETURN```

The random number that determines the strength of the enemy is the second one in the series that starts with F. If the player flees and returns, then F will not have changed, there will still be an encounter, and the strength of the enemy will be the same. Notice that the type of monster is determined by the terrain the player is in. As mentioned before, this is just flavour text and doesn't affect how the monsters fight. I'll leave combat to the next blog post.