Kingdom of the Lyre: Terrain
Tuesday, 12th November 2019
In this series, Damian blogs about how the game works.
After path generation, I turned to terrain generation. Terrain in Kingdom of the Lyre ended up being just flavour text, and has no effect on the progress of the game. Perhaps if I expand the game in future I might vary the monsters a bit more, and make some potions more prevalent in some biomes. Here's the routine that generates terrain for a location:
REM --- Get the terrain of a location REM --- Inputs: X - origin location REM --- Changes: C - counter REM --- Y - latitude REM --- Z - random number seed REM --- Outputs: T - terrain at the current square REM --- 0 = plain REM --- 1 = forest REM --- 2 = mountain REM --- 3 = swamp 300 LET C=0 LET T=0 LET Z=4882 LET Y=X/80 LET X=X-80*Y REM --- Get terrain for the general area and its surroundings 301 GOSUB 990 IF Y<72 THEN IF C=(X/8*8)+80*(Y/8*8+8) THEN LET T=T+256*(Z-Z/4*4) IF X>7 THEN IF C=(X/8*8-8)+80*(Y/8*8) THEN LET T=T+64*(Z-Z/4*4) IF Y>7 THEN IF C=(X/8*8)+80*(Y/8*8-8) THEN LET T=T+16*(Z-Z/4*4) IF X<72 THEN IF C=(X/8*8+8)+80*(Y/8*8) THEN LET T=T+4*(Z-Z/4*4) IF C=(X/8*8)+80*(Y/8*8) THEN LET T=T+(Z-Z/4*4) LET C=C+1 IF C<=X+80*Y+8 THEN GOTO 301 REM --- Narrow it down for this specific square GOSUB 990 IF Y>Y/8*8+4 THEN IF Z-Z/8*8<Y-(Y/8*8+4) THEN LET T=T/256+T/4*4 IF Y<Y/8*8+3 THEN IF Z-Z/8*8<(Y/8*8+3)-Y THEN LET T=T/16-T/64*4+T/4*4 GOSUB 990 IF X<X/8*8+3 THEN IF Z-Z/8*8<(X/8*8+3)-X THEN LET T=T/64-T/256*16+T/4*4 IF X>X/8*8+4 THEN IF Z-Z/8*8<X-(X/8*8+4) THEN LET T=T/4-T/16*4+T/4*4 LET T=T-T/4*4 LET X=X+80*Y RETURN
As before, line 990 is just the Random Number Generator. This algorithm splits the map up into 100 "general areas", each 8x8 locations in size. It ascertains which of these areas a coordinate lies in, and generates a random terrain type for it, and for the areas north, south, east and west.
When it comes to ascertain the terrain type of the particular location, it works out which edge of its area it is closest to, and how close. A location in the centre of its area has a 100% chance of taking that area's terrain type. A location at the edge has a 50% chance of taking the area's terrain type, and a 50% chance of taking the neighbouring terrain type.
This fuzziness at the edge of each defined area generates terrain where one biome will blend gradually into the next. Forests will get more dense towards the middle, there will be foothills at the edge of mountains, and so on. The high probability of finding two or more of the same biome bordering one another gives some interesting shapes.
When I rendered the terrain into a graphical map I saw a few areas with horizontal borders between one biome and the next. I think these are too many to be coincidence, but if there is an error in the code above (as opposed to some unfortunate pattern in the random number generator) then I was unable to find it in the time available during PROCJAM.
Notice that for terrain generation I use a different fixed seed from that which path generation uses. Other parts of the game use seeds that change during the game; one example is encounters which I'll cover in the next blog post.
Comments