Site logo,

The Forest - orienteering simulation

The program described here demonstrates a technique for generating vast terrain by an algorithm rather than storing it in memory or having to download it as a huge amount of data.

The version available here is written in JavaScript for an HTML5 page and occupies less than 100 kilobytes, so it downloads and runs almost instantly.

The Forest

I first used the method in the early 1980s in a program for the Sinclair ZX Spectrum, a machine having only 48 kilobytes of useable memory. That program simulated the sport of orienteering quite successfully despite the severe limitations of the technology. More details can found in the History section below, which includes a link to an archive for obtaining the old program and running it on a Spectrum emulator.

I am keen for other programmers to learn about the technique and perhaps take it further, so the source code of the new program is easily viewed.

 The (new) Forest

There are three views of the terrain available in The Forest:

This text is from

Although there are buttons for switching between these views there are also keyboard alternatives indicated on the buttons.

 Map and 3D plot

The map has 4 states, selectable through a button:

This text is from

All show north lines 300m apart.

Printed orienteering maps declare their scale and contour interval but we cannot do that here. The scale depends on the size of your display screen. However, 1 pixel on the map represents 1 metre on the ground and the initial map is 800 pixels wide and 600 high. The north lines indicate the scale because they are 300m apart. The vertical scale in the scene display is rather arbitrary, so it is not possible to say what the contour interval is either. Just assume it is the usual 5m.

The map can be rotated clockwise in steps of 90°. North lines on the map have black arrows so that when the map has been rotated the direction of north is clear. Some orienteers find it easier to relate the map to the ground if it is oriented roughly to the direction they are facing.

The aim of the 3D plot is to help people interpret contour lines on the map. It may take several seconds to draw the plot - rather longer than the scene or map views.

 The scene view

The scene view indicates what the observer would see from an eye level 1.5m above the ground. The view indicates whether the ground ahead slopes up or down, according to the contours on the map. The type of terrain ahead is indicated by graphics. One of the advances here is that real photographic images can be used rather than simple drawn graphics. The ground is drawn in true perspective out to a certain distance ahead (the visible range). Initially this range is set to 30m but you can select greater ranges. You will soon find that if the range is too great it will take an appreciable time to draw the scene (depending on the speed of the processor in your computer). For moving through the forest it is desirable to keep the scene drawing time to less than a tenth of a second (100ms) The drawing time is shown in a status line below the display.

The arrow keys are active in the scene and they act as follows.

NB: The arrow keys may scroll/pan the window in some browsers or on small screens, so letter key alternatives are allowed: initials of Right, Left, Up , Down (upper or lower case).

Note that the observer's current position (x, y) and bearing (degrees clockwise from due North) are shown in the status line at the bottom of the view. A compass is also shown at the bottom centre of the scene view.

Why do level things such as lakes appear to slope down at the edges of the scene? Because we are viewing a circular disc patch of ground from a small eye-level distance above it.


The observer mentioned above may have one of several roles:

 The easy short course

 At the finish

When an orienteer completes a course the full control card and a table analysing the route are shown:


 Further information

There are some pages about my orienteering maps of real forests here. Follow the links there to read more about the original ZX Spectrum simulation of orienteering.

In particular there is a page about why The Forest is designed the way it is, which I wrote in the mid-1980s. It still has some relevance, especially about why I do not attempt to use maps of real forests (a scarce resource in the UK).

I started developing the new HTML5/JavaScript version in 2014 but it was heading more towards my second Spectrum program, Explorer (published by Electric Dreams in the mid-1980s). It included the possibility of falling down mineshafts and navigating underground. It also included a teleportation feature whereby typing a string of characters would take you to a new position on the map, seemingly at random but the same string would always go to the same place. Having got those features to work I rather lost interest in that development and shelved it.

My interest began again in February 2018 with an email out of the blue from Graham Scott of NATO (Newcastle and Tyneside Orienteers). He and I had been members of Tyneside Orienteers (as it was then) more than 30 years ago. I decided to complete the program as a purely orienteering project. The Forest


The original versions of The Forest and Explorer were written in Z80 assembler, in order to fit into the very limited amount of memory available and to run at a reasonable speed (much faster than interpreted BASIC). The new version at is written in JavaScript, a much more easily comprehended language, running within an HTML5 page and doing the graphics within <canvas> elements. If you right-click and "view source" on that page you can see the file names for the JavaScript files and therefore view them also in your browser, to see how it works. If you are not familiar with JavaScript, particularly its object-oriented aspects, you may wish to peruse my introductory programming course.

Note again that the program runs entirely in your browser. After downloading the program and images the server has no further involvement. The browser does all of the hard work of course, and that is a very much larger program.

The contours and terrain types repeat after 33km (32768m to be precise; older programmers will recognise that number instantly) but the point features do not repeat like that.

 Remaining work

March 2018: All of the point features are now shown but rather crudely; it remains to draw them better. I need some photos of knolls, depressions, mineshafts and ponds. Otherwise the program is functionally complete but I am open to ideas for further enhancements. I am amazed by how much detail it is now possible to show in real time by writing some JavaScript and running it in a standard browser.

 Development environment

I am developing the program on a Microsoft Surface Pro 4 running 64-bit Windows 10. I use Netbeans 8.2 IDE (free Integrated Development Environment) with its HTML5/JavaScript plug-in kit, mainly because I already had it for Java programming. An IDE is not essential for this work but it does offer suggestions as I type and it does point out syntax errors immediately. When I am unsure about any JavaScript detail I use the Mozilla reference pages which I find more thorough and up-to-date than the old w3schools site. I test the program by loading the HTML file into the Firefox Quantum browser. I do not use a localhost server (no need). Firefox has a very useful Web Console (under the Web developer menu, or key Ctrl+Shift+K) which gives me the script file name and the line number at which any run-time error is detected. I upload the tested files to my web site using Filezilla FTP (free). To avoid possible probems with script caching in client browsers I increment a suffix letter on the name of each script file that changes for a new version. That way the user would only need to refresh the HTML page to ensure that all the correct script versions are loaded.

I have only tested The Forest in Firefox, Edge and Internet Explorer 11. Timings for drawing are fine in all 3 browsers. I would welcome feedback (gr<at>grelf<dot>net) on performance in other browsers: how long does each take to draw the initial scene, map and 3D plot, as show in a status line below the graphics? Average of 3 readings please (times vary due to the browser doing other things).


In May 2014 I had a blast from the past about some games I wrote for the Sinclair ZX Spectrum and similar machines 30 years earlier. It seems that the current resurgence of interest in popular programming may be reviving the ideas behind the games. I had a particular technique of my own for generating terrain on a large enough scale to be explorable, despite the lack of memory in those early machines (the Spectrum had only 48k bytes - and that was the larger version). Indeed, one of my games was simply called Explorer. Its predecessor, The Forest, was a simulation of the sport of orienteering.

Explorer was developed as more of a team effort. The programming was still all mine but the graphics were created by Simon Dunstan, of the RamJam Corporation in London. He was able to do wonderful things in the extremely limited graphical environment of the Spectrum. RamJam got the program published through Electric Dreams.

There is a web site called World of Spectrum where a group of people are attempting to assemble an archive of all programs written for the ZX Spectrum and make many of them available to download and run in one of the many Spectrum simulators that are now also available for free. They have listed my two programs here (they have promised to correct the spelling of my name).

There is also an interesting article looking back at Explorer on Mark Green's blog, Pixelatron.

Technology has advanced so much in 30 years that much better versions of such games are now practical. For one thing processor clock speeds have gone up by a factor of nearly 1,000 - the Spectrum ran (I think) at 4 MHz. It is common now to have multiple processors in a single machine (eg, the Intel Core i5 etc), giving another potential speed increase. Also the processing of graphics is now usually hived off to a separate Graphics Processing Unit (GPU) rather than the Central Processing Unit having to do everything, as was the case all those years ago. Today's processors also have special instructions built into their hardware for frequently used computations. I think it was in the mid-1980s that Intel processors (such as 80286) started to have companion chips called coprocessors, for doing non-integer arithmetic. Devices on silicon have now been so much further miniaturised that all such work can now be done on a single chip rather than requiring a coprocessor.

It is now common to have gigabytes of main memory. The Surface tablet/laptop on which I am writing this has 16 Gbytes of RAM, which is 334,000 times that of the Spectrum. It almost goes without saying that we can now have very much better graphics, of photographic quality, something I could only dream of in the 1980s.

I have shown that it is possible to re-implement the Forest/Explorer games in HTML5 with JavaScript, making them available on just about any platform that can browse the web. Despite the fact that JavaScript is run interpretively by the browser, which is in principle much less efficient than the assembly code I wrote for the Spectrum's Z80 microprocessor, speeds are still much better than 30 years ago. The HTML5/JavaScript version can be seen here: It runs entirely in the browser, there is nothing executed on the server. The entire program is less than 100 kilobytes, so it downloads and starts almost instantly (unlike some Steam programs that take many hours to download because they include vast amounts of data). The JavaScript has not been compacted or compressed because I want others to be able to understand the techniques and perhaps develop them further. This text is from

 Earlier writings

In 1982 I had two articles published in the now long-defunct British magazine "Practical Computing". The articles described the basic techniques I was using to generate terrain from mathematical functions rather than storing it in memory. Following here are links to scanned images of the articles.

 March 1982

Cover image

Page 93

Page 95

 September 1982

Cover image

Page 127

Page 129

Page 130

Next page